怎么知道一个字符串是否只包含java中的数字? [英] How know if a string contains only numbers in java?
本文介绍了怎么知道一个字符串是否只包含java中的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要知道Java中的String,它作为参数输入,是否只包含数字。我知道存在一个String类的方法,让你检测到它。
我尝试了什么:
我试图使用Ascii代码,条件和包含String的每个字符的ArrayList来比较每个字符,但是这个解决方案效率很低。
I need to know if a String in Java, that it enters as a parameter, contains only numbers. I know that exists a method of String class, that let's you detect that.
What I have tried:
I tried to compare each character using Ascii code, conditionals and an ArrayList that contains each character of the String, but this solution is very inefficient.
推荐答案
使用正则表达式:课程:正则表达式(Java™教程>基本类) [ ^ ]
然后它是微不足道的:
Use a Regex: Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)[^]
Then it's trivial:
^\d+
是所有模式你需要。
Java
轻轻提供 Character.isDigit [ ^ ]方法。你可以反复调用它。
Java
gently provides the Character.isDigit[^] method. You may call it iteratively.
这篇关于怎么知道一个字符串是否只包含java中的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文