1

I have a string, such as "4.25GB"

  1. I'd like to get the floating part "4.25"
  2. And get the string part "GB"

How to get the two values respectively in Java.

Thanks.

2
  • If it always has to be the last 2 characters to be string, then you can get all of the characters before them as double (or float) and the remaining characters as string. Commented Dec 25, 2014 at 8:47
  • Character.isDigit() or Character.isLetter() will help you to identify the letter and numbers Commented Dec 25, 2014 at 8:52

9 Answers 9

2

Try

String s = "4.25GB"
Float value = Float.valueOf(s.replaceAll("[^\\d.]", "")); // remove all non-numeric symbols
String f = s.replaceAll("[0-9]",""); // remove all numbers
Sign up to request clarification or add additional context in comments.

Comments

1

To get Number Part: String numberPart = "4.25GB".replaceAll("[^0-9.]", "");

To get String part: String stringPart = "4.25GB".replaceAll("[^A-Za-z]", "");

Comments

0

Use String.replaceAll to first replace all non-digits and dot with "" to get the number then otherwise

Comments

0

You can write a function that will be similar to C# int.TryParse method, and use it in loop on your string, it will only work if you alwayes have a (NUM)(STRING) formation :

boolean tryParse(String value)  
{  
     try  
     {  
         Integer.parseInt(value);  
         return true;  
      } catch(NumberFormatException e)  
      {  
          return false;  
      }  
}

Comments

0

Use split/ substring concept. divide the string like below:

 String Str = new String("4.25GB");
 for (String retval: Str.split("G")){
     System.out.println(retval);
  }

       //or u can use 
   String[] r = s.split("(?=\\p{Upper})");

Comments

0

You could use public String substring(int beginIndex, int endIndex)

String start = "4.25GB";
String numbers = start.substring(0,4);
String letters = start.substring(4,6);

Read more about substrings and how to use them here

Comments

0

Tested, works:

String str = "4.25GB" ;
String parts[] = str.split("(?i)(?<=\\d)(?=[a-z])|(?<=[a-z])(?=\\d)");

float number = Float.parseFloat(parts[0]) ;
String string = parts[1] ;

System.out.println(number); //4.25
System.out.println(string); //GB

Comments

0

You can use regular expression like this :

String s = "4.25GB";

String num = s.replaceAll("[^0-9.]", "");
System.out.println(num);
String str = s.replaceAll("[0-9.]", "");
System.out.println(str);

wish help you.

Comments

0

That depends on what "such as" means. Are all the strings in the format "x.xxGB"? If that's the case, then you can use substring(), as you know the exact number of 'float' chars and 'suffix' chars.

String theStr = "x.xxGB";
String numStr = theStr.substring(0, 4); // grab first 4 chars: "x.xx"
float numFloat = Float.parseFloat(numStr);
String suffix = theStr.substring(5);  //  or .substring(5, 7);  if you know the exact length

If it's more variable than that, it gets more complicated. If you don't know the length of the leading number string, you'd have to check the first part as a valid float, with perhaps the easiest way to be gathering characters as the start and checking each succession as a valid float, with all the rest being considered a suffix. Maybe something like this (pseudocode-ish):

String theStr = "324.994SUFFIX";  // SomeArbitraryNumberAndSuffixString
String currNumStr = "";
Boolean bHaveFloat = true;
for (int i = 1; i < theStr.length(); i++){
    String testStr = theStr.substring(0, i);
    try{
        float f = Float.parseFloat(testStr);
    } catch (NumberFormatException nfe){
        // handle the exception, printStackTrace, etc...
        // failed so No longer have Valid String...
        break;
    }
    currNumStr = testStr;
}
//  currNumStr now has the valid numberString

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.