java 使用 isDigit() 时遇到问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4152809/
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
Trouble using isDigit()
提问by Jon Skeet
I am trying to use isDigit() to verify if a character in a string is not a digit.
我正在尝试使用 isDigit() 来验证字符串中的字符是否不是数字。
if (aString.charAt(i).isDigit == false)
I am getting the error: cannot invoke isDigit on the primitive type character. What am I doing wrong?
我收到错误:无法在原始类型字符上调用 isDigit。我究竟做错了什么?
回答by Jon Skeet
It's a static method in the Character
class, so you need:
它是类中的静态方法Character
,因此您需要:
if (!Character.isDigit(aString.charAt(i)))
(Note the use of !
instead of comparing the result with false
, btw. Both ways will work, but I view the above as more idiomatic.)
(请注意使用!
而不是将结果与false
,进行比较。两种方法都可以,但我认为以上更符合习惯。)