java 获取字符串“600sp”的整数部分的最佳方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3552756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:22:44  来源:igfitidea点击:

Best way to get integer part of the string "600sp"?

javastringintegertype-conversion

提问by Brad Hein

I have a string, say "600sp" from which I wish to obtain the integer part (600).

我有一个字符串,比如说“600sp”,我希望从中获得整数部分(600)。

If I do Integer.valueOf("600sp")I get an exception due to the non-numeric value "s" which is encountered in the string.

如果我这样做,Integer.valueOf("600sp")由于字符串中遇到的非数字值“s”,我会得到一个异常。

What is the fastest cleanest way to grab the integer part?

获取整数部分的最快最干净的方法是什么?

Thanks!

谢谢!

回答by Chuk Lee

If your string format is always going to be number followed by some characters, then try this

如果您的字符串格式总是数字后跟一些字符,那么试试这个

mystr.split("[a-z]")[0]

回答by Jherico

Depending on the constraints of your input, you may be best off with regex.

根据您输入的限制,您可能最好使用正则表达式。

    Pattern p = Pattern.compile("(\d+)");
    Matcher m = p.matcher("600sp");
    Integer j = null;
    if (m.find()) {
        j = Integer.valueOf(m.group(1));
    }

This regular expression translates as 'give me the set of contiguous digits at the beginning of the string where there is at least 1 digit'. If you have other constraints like parsing real numbers as opposed to integers, then you need to modify the code.

这个正则表达式翻译为“在字符串的开头给我一组连续的数字,其中至少有 1 位数字”。如果您有其他限制,例如解析实数而不是整数,那么您需要修改代码。

回答by Pablo Fernandez

For a shorter and less specific solution, add more context.

对于较短且不太具体的解决方案,请添加更多上下文。

StringBuffer numbers = new StringBuffer();
for(char c : "asdf600sp".toCharArray())
{
  if(Character.isDigit(c)) numbers.append(c);
}

System.out.println(numbers.toString());

In the light of the new info, an improved solution:

根据新信息,改进的解决方案:

Integer.valueOf("600sp".replace("sp",""));

回答by Topera

You can use

您可以使用

Integer.valueOf("0" + "600sp".replaceAll("(\d*).*", ""))


Note:

笔记:

With this regex you will keep only the initial numbers.

使用此正则表达式,您将只保留初始数字。



Edit: The "0" +is used to not crash when i have no digits. Tks @jherico!

编辑:"0" +当我没有数字时,它用于不崩溃。Tks @jherico!

回答by D.Shawley

I know that this has already been answered, but have you considered java.util.Scanner? It seems to fit the bill perfectly without regex's or other more complex string utilities.

我知道这已经得到了回答,但是您考虑过java.util.Scanner吗?如果没有正则表达式或其他更复杂的字符串实用程序,它似乎完全符合要求。

回答by Thilo

If the string is guaranteed (as you say it is) to be an integer followed by "sp", I would advise against using a more generic regular expression parser, which would also accept other variations (that should be rejected as errors).

如果字符串被保证(如你所说)是一个整数后跟“sp”,我会建议不要使用更通用的正则表达式解析器,它也会接受其他变体(应该作为错误被拒绝)。

Just test if it ends in "sp", an then parse the substring without the last two characters.

只需测试它是否以“sp”结尾,然后解析没有最后两个字符的子字符串。