可以通过捕捉异常来实现。
String strSource = "abcd12345";
try
{
int iResult = Integer.parseInt(strSource);//如果是數字字符串不会抛出异常
//double,float类似.
}
catch(Exception eNumber)
{
System.err.println("出错:"+eNumber);
}
String strSource = "abcd12345";
try
{
int iResult = Integer.parseInt(strSource);//如果是數字字符串不会抛出异常
//double,float类似.
}
catch(Exception eNumber)
{
System.err.println("出错:"+eNumber);
}
boolean isNumber(String s) {
}
如果发现字串中有符合下列条件的
[^0-9]
则含有非数字字串。
(find("[^0-9]","13312412344513241234123"))public static boolean find(String Pattern,String Matcher){
java.util.regex.Pattern p=java.util.regex.Pattern.compile(Pattern);
java.util.regex.Matcher m=p.matcher(Matcher);
return m.find();
}
try{
if ( ! (new Double(s)).isNaN() ) { //s is a string System.out.println("It's a number.");
}
}catch(Exception e){ System.out.println("It's not a number.");
}
public boolean isNumberStr(String s)
{
boolean result = false;
try
{
Integer number = new Integer(s);
result = true;
}catch (Exception ex)
{
System.out.println("It's not a number string: " + s);
result = false;
}
return result;
}