在String中提取整数部分 [英] Extract Integer Part in String

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

问题描述

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

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

Hello123

你如何获得123部分。您可以使用Java的扫描仪来破解它,有更好的方法吗?

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.

看看< a href =http://www.regular-expressions.info/tutorial.html =noreferrer> http://www.regular-expressions.info/tutorial.html 了解正则表达式的表达方式工作。

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

编辑:我想说Regex可能对这个例子有点过分,如果其他提交者发布的代码确实有效......但是我仍然建议一般学习正则表达式,因为它们非常强大,并且会比我想要承认的更方便(在等了好几年之后才给它们开枪)。

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

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

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