String ="(Buy) 655500 - (Sell) 656500";
I want to split this string by ignoring (Buy)
, -
, (Sell)
.
The final result I want is like this 655500 656500
.
Above is the example..Actually my string contain UTF-8 characters..But I left it here
String ="(Buy) 655500 - (Sell) 656500";
I want to split this string by ignoring (Buy)
, -
, (Sell)
.
The final result I want is like this 655500 656500
.
Above is the example..Actually my string contain UTF-8 characters..But I left it here
Regular Expression
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());
}
String string = "(Buy) 655500 - (Sell) 656500";
String needed = string.replaceAll("[\"(Buy)(Sell)-]", "");
this should work maybe ... needed is the String that should give you the needed result.
Your best approach would be to define a regular expression pattern match for the sequence and extract the values from that.
If your syntax is always like that you can just split like that :
String string = "(Buy) 655500 - (Sell) 656500";
String replaced= string.replaceAll("[(Buy)(Sell)]", "");
String[] values = replaced.split("-");
here : value[0] will be 655500 and values[1] will be 656500
If Your requirement is different then comment.
another way:
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);
and here's the output:
Base String: (Buy) 655500 - (Sell) 656500
Buy String : 655500
Sell String: 656500
Try this:
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);
Fnd you will get list of numbers in your string.