1

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?

2
  • 3
    What have you tried? Please show some effort. Commented Dec 27, 2012 at 11:03
  • Double.valueOf(s.split("\s")[0]) Commented Dec 27, 2012 at 11:04

3 Answers 3

4
String example = "600 test";
return Double.parseDouble ( example.substring ( 0, example.indexOf(" ")));
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.