Eric Tseng
Eric Tseng

Reputation: 563

Java: Get Float and String value from a String

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.

Upvotes: 1

Views: 1639

Answers (9)

CodaRuner
CodaRuner

Reputation: 505

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

Upvotes: 0

micneer
micneer

Reputation: 16

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.

Upvotes: 0

S1LENT WARRIOR
S1LENT WARRIOR

Reputation: 12224

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

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

Upvotes: 1

sybear
sybear

Reputation: 7784

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

Upvotes: 0

James
James

Reputation: 338

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

Upvotes: 0

Sergey Shustikov
Sergey Shustikov

Reputation: 15821

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

Upvotes: 2

Abhi Urs
Abhi Urs

Reputation: 95

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

Upvotes: 0

Roizman
Roizman

Reputation: 11

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

Upvotes: 0

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136062

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

Upvotes: 0

Related Questions