我想检查字符串是否为正自然数,但我不想使用Integer.parseInt()
,因为用户可能输入大于int的数字。相反,如果数字字符串包含全部“0”字符,我宁愿使用正则表达式来返回false。用于检查字符串是否为正数的正则表达式
if(val.matches("[0-9]+")){
// We know that it will be a number, but what if it is "000"?
// what should I change to make sure
// "At Least 1 character in the String is from 1-9"
}
注:该字符串必须只包含0
- 9
,它必须不包含所有0
秒;换句话说,它必须在[1-9]
中至少有1个字符。
你能澄清你的意思是“正数”吗? 2.5是一个正数吗?那么'π'呢? –
更新 - >正自然数 – Tum