2013-12-16 33 views
-2
String ="(Buy) 655500 - (Sell) 656500"; 

我想通过忽略(Buy),-,(Sell)来拆分这个字符串。如何分割一个复杂的字符串?

我想要的最终结果就是这样655500 656500

以上是example..Actually我的字符串包含UTF-8 characters..But我离开这里

+0

您能否让我们知道您到目前为止尝试解决这个问题? – MansoorShaikh

+0

如果你的语法总是这样,你可以只分割空白,并总是知道数组的索引... – Ben

+0

如果答案的工作,请给予接受答案:) –

回答

4

正则表达式

String src = "(Buy) 655500 - (Sell) 656500"; 
    String reg = "[0-9]+"; 
    Pattern pattern = Pattern.compile(reg); 
    Matcher matcher = pattern.matcher(src); 
    while(matcher.find()) { 
     System.out.println(matcher.group()); 
    } 
+0

我认为这是最好的兼容我的代码..使用正则表达式,这是最好的UTF8字符条件..感谢Longwayto – lynndragon

1
String string = "(Buy) 655500 - (Sell) 656500"; 

String needed = string.replaceAll("[\"(Buy)(Sell)-]", ""); 

这应该工作也许......需要的是应该给你所需要的结果的字符串。

0

如果你的语法总是这样,你只能分割这样的:

String string = "(Buy) 655500 - (Sell) 656500"; 
String replaced= string.replaceAll("[(Buy)(Sell)]", ""); 
String[] values = replaced.split("-"); 

这里: 值[0]将是655500个 和值[1]将656500

如果您的要求不同,然后评论。

0

另一种方式:

String baseStr = "(Buy) 655500 - (Sell) 656500"; 
String buy = baseStr.split("-")[0].replaceAll("\\D+", ""); 
String sell = baseStr.split("-")[1].replaceAll("\\D+", ""); 

System.out.println("Base String: " + baseStr); 
System.out.println("Buy String : " + buy); 
System.out.println("Sell String: " + sell); 

和这里的输出:

Base String: (Buy) 655500 - (Sell) 656500 
Buy String : 655500 
Sell String: 656500 
0

试试这个:

String text = "(Buy) 655500 - (Sell) 656500"; 
    List<String> parts = new LinkedList<String>(Arrays.asList(text.split("\\D"))); 
    parts.removeAll(Arrays.asList("")); 
    System.out.println(parts); 

测量值,你会得到你的串号的清单。