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

发布于 2024-09-15 20:42:18 字数 159 浏览 6 评论 0原文

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

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

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

谢谢!

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

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

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

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

(り薆情海 2024-09-22 20:42:18

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

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

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

mystr.split("[a-z]")[0]
逆光飞翔i 2024-09-22 20:42:18

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

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

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

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.

つ可否回来 2024-09-22 20:42:18

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

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

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

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

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

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",""));
-小熊_ 2024-09-22 20:42:18

您可以使用

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

注意:

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


编辑:
"0" + 用于在我没有数字时不会崩溃。谢谢@jherico!

You can use

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

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!

﹏雨一样淡蓝的深情 2024-09-22 20:42:18

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

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

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).

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

鯉魚旗 2024-09-22 20:42:18

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文