Reputation: 293
I have a string and I would like to split the values seperately.
For instance, I would like to split the following string:
Test1 Avg. running Time: 66,3 [ms], (Ref: 424.0) ===> Well done, It is 80% faster
I would want 66,3[ms] seperately and the Ref value seperately.
It would be helpful if any of you could suggest me which will eb the best way to do this.
Should I use a delimiter(:)? But in this case, I receive the output as
66,3 [ms], (Ref: 424.0) ===> Well done, It is 80% faster
Or should I use a 'regex'?
Upvotes: 1
Views: 5295
Reputation: 5811
You can try this regex:
String test = "Test1 Avg. running Time: 66,3 [ms], (Ref: 424.0) ===> Well done, It is 80% faster";
Pattern p = Pattern.compile("(\\d+[.,]?\\d+)");
Matcher m = p.matcher(test);
m.find();
String avgRunningTime = m.group(1);
m.find();
String ref = m.group(1);
System.out.println("avgRunningTime: "+avgRunningTime+", ref: "+ref);
This would print:
avgRunningTime: 66,3, ref: 424.0
You will naturally want to add some error checking (e.g. check if m.find()
returns true
).
Upvotes: 0
Reputation: 1555
use this way
public class JavaStringSplitExample {
public static void main(String args[]) {
String str = "one-two-three";
String[] temp;
String delimiter = "-";
temp = str.split(delimiter);
for (int i = 0; i < temp.length; i++)
System.out.println(temp[i]);
/*
* NOTE : Some special characters need to be escaped while providing
* them as delimiters like "." and "|".
*/
System.out.println("");
str = "one.two.three";
delimiter = "\\.";
temp = str.split(delimiter);
for (int i = 0; i < temp.length; i++)
System.out.println(temp[i]);
/*
* Using second argument in the String.split() method, we can control
* the maximum number of substrings generated by splitting a string.
*/
System.out.println("");
temp = str.split(delimiter, 2);
for (int i = 0; i < temp.length; i++)
System.out.println(temp[i]);
}
}
Upvotes: 0
Reputation: 33534
You can use split()
function...
String s = "66,3 [ms], (Ref: 424.0) ===> Well done, It is 80% faster";
String[] arr = s.split(", ");
Upvotes: 0