2

I want to split this string

String info = "0.542008835 meters height from ground";

from this i want to get only two decimals like this 0.54.

by using this i am getting that

String[] _new = rhs.split("(?<=\\G....)");

But i am facing problem here, if string does't contain any decimals like this string

String info = "1 meters height from ground";

for this string i am getting those characters upto first 4 in the split string like 1 me. i want only numbers to split if it has decimals, How to solve this problem.

RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166

5 Answers5

7
if(info.contains("."))
{
       String[] _new = rhs.split("(?<=\\G....)");
}
Deepzz
  • 4,573
  • 1
  • 28
  • 52
  • now i am facing a problem, when that string is like this "406.9881 meters height from ground' i am able to get split like this 406. but i want upto two decimals like this 406.98 – RajaReddy PolamReddy Feb 01 '13 at 11:29
  • @RajaReddy PolamReddy :ok then try this... `String[] _new = rhs.split(".");` u'll get _new[1] as the value before decimal.. then u can get 1st 2 letters from the string _new[2].. and then append the 2 strings.. – Deepzz Feb 01 '13 at 11:33
  • Both are working well, thanks madam. no need to think your answer is not better. both are good. – RajaReddy PolamReddy Feb 01 '13 at 12:09
3

I think you can check by white space after first value. see this If you get the space then get first character only.

For checking if a string contains whitespace use a Matcher and call it's find method.

Pattern pattern = Pattern.compile("\\s");
Matcher matcher = pattern.matcher(s);
boolean found = matcher.find();

If you want to check if it only consists of whitespace then you can use String.matches:

boolean isWhitespace = s.matches("^\\s*$");
Community
  • 1
  • 1
Chintan Khetiya
  • 15,962
  • 9
  • 47
  • 85
2

You could use a regex to do this as an alternative to Deepzz's method, this will handle the case where there is a '.' in the later part of the String, I've included an example below. It's not clear from your question is you actually want to remaining part of the String, but you could add a second group to the reg ex to capture this.

public static void main(String[] args) {
    final String test1 = "1.23 foo";
    final String test2 = "1 foo";
    final String test3 = "1.234 foo";
    final String test4 = "1.234 fo.o";
    final String test5 = "1 fo.o";
    getStartingDecimal(test1);
    getStartingDecimal(test2);
    getStartingDecimal(test3);
    getStartingDecimal(test4);
    getStartingDecimal(test5);

}

private static void getStartingDecimal(final String s) {
    System.out.print(s + " : ");
    Pattern pattern = Pattern.compile("^(\\d+\\.\\d\\d)");
    Matcher matcher = pattern.matcher(s);
    if(matcher.find()) {
        System.out.println(matcher.group(1));
    } else {
        System.out.println("Doesn't start with decimal");
    }
}
Kingamajick
  • 2,281
  • 1
  • 16
  • 19
1

Assuming the number is always the first part of the string:

String numStr = rhs.split(" ")[0];
Double num = Double.parseDouble(numStr);

After that you can use the String Formatter to get the desired representation of the number.

Paul
  • 5,163
  • 3
  • 35
  • 43
1

This will work when you know the String near the numbers, with int and double numbers as well.

    String a ="0.542008835 meters height from ground";
    String b = a.replace(" meters height from ground", "");
    int c = (int) ((Double.parseDouble(b))*100);
    double d = ((double)c/100);
Arnold
  • 273
  • 1
  • 11
  • this is string meters height from ground is not common for all values – RajaReddy PolamReddy Feb 01 '13 at 11:27
  • But i think that is an auto generated string, ansd you have a few form, but thats is exact. Then try to replace all if you have like 5. Faster than if you check all options, checking the .-s all the time. – Arnold Feb 01 '13 at 11:34