简体   繁体   English

Java:从String获取Float和String值

[英]Java: Get Float and String value from a String

I have a string, such as "4.25GB" 我有一个字符串,例如"4.25GB"

  1. I'd like to get the floating part "4.25" 我想要浮动部分"4.25"
  2. And get the string part "GB" 并获得字符串部分"GB"

How to get the two values respectively in Java. 如何在Java中分别获取这两个值。

Thanks. 谢谢。

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

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

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

使用String.replaceAll首先替换所有非数字,使用“”替换“”以获取数字,否则

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 : 你可以编写一个类似于C#int.TryParse方法的函数,并在你的字符串循环中使用它,只有当你总是有一个(NUM)(STRING)形式时它才会起作用:

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

Use split/ substring concept. 使用split / substring概念。 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})");

You could use public String substring(int beginIndex, int endIndex) 你可以使用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 阅读有关substrings以及如何在此处使用它们的更多信息

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

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. 希望能帮助你。

That depends on what "such as" means. 这取决于“如”这意味着什么。 Are all the strings in the format "x.xxGB"? 所有字符串的格式是“x.xxGB”吗? If that's the case, then you can use substring(), as you know the exact number of 'float' chars and 'suffix' chars. 如果是这种情况,那么你可以使用substring(),因为你知道'float'chars和'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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM