Reputation: 534
I get a string that consists of a number and then some text, eg; "23 test", "600 tests test" the string will always start with the number but it has no set length. How do i get the number value and double it?
Upvotes: 1
Views: 475
Reputation: 2847
I would do the following.
//Assumption that there is always a space after the number. Lets say String string = "23 test";
String stringArray = string.split("");
String number = stringArray[0];
System.out.print(Integer.parseInt(number)*2);
Upvotes: 0
Reputation: 16345
split on space, take the value on 0th index, convert it into Integer and double the value
String s = "23 test";
Integer.parseInt(s.split(" ")[0]) * 2
Please make sure that all strings follow the same format else there will be a numberFormatException
Upvotes: 0
Reputation: 10874
String example = "600 test";
return Double.parseDouble ( example.substring ( 0, example.indexOf(" ")));
Upvotes: 4