提取字符串中的整数部分 [英] Extract Integer Part in String

查看:33
本文介绍了提取字符串中的整数部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提取字符串整数部分的最佳方法是什么

What is the best way to extract the integer part of a string like

Hello123

123 部分是怎么得到的.您可以使用 Java 的 Scanner 对其进行破解,有没有更好的方法?

How do you get the 123 part. You can sort of hack it using Java's Scanner, is there a better way?

推荐答案

为什么不直接使用正则表达式来匹配您想要的字符串部分?

Why don't you just use a Regular Expression to match the part of the string that you want?

[0-9]

这就是你所需要的,加上它需要的任何周围字符.

That's all you need, plus whatever surrounding chars it requires.

查看http://www.regular-expressions.info/tutorial.html 了解正则表达式的工作原理.

Look at http://www.regular-expressions.info/tutorial.html to understand how Regular expressions work.

我想说这个例子中的正则表达式可能有点过分,如果其他提交者发布的代码确实有效......但我仍然建议总体上学习正则表达式,因为它们非常功能强大,而且比我想承认的更能派上用场(等了几年才给他们一个机会).

I'd like to say that Regex may be a little overboard for this example, if indeed the code that the other submitter posted works... but I'd still recommend learning Regex's in general, for they are very powerful, and will come in handy more than I'd like to admit (after waiting several years before giving them a shot).

这篇关于提取字符串中的整数部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆