java中String是数字的话转Int

时间:2025-02-14 20:15:55

1.用正则表达式
首先要import 和

/**
     * 利用正则表达式判断字符串是否是数字
     * @param str
     * @return
     */
    public boolean isNumeric(String str){
           Pattern pattern = ("[0-9]*");
           Matcher isNum = (str);
           if( !() ){
               return false;
           }
           return true;
    }
/**
		 * 判断字符串是否是数字,如果是数字就返回,否则返回0
		 * @param str
		 * @return
		 */
		public static Long getInt(String str){
			 Pattern pattern = ("[0-9]*");
	         Matcher isNum = (str);
	         if( !() ){
	             return 0L;
	         }
	         return (str);
		}


2.用JAVA自带的函数

public static boolean isNumeric(String str)
{
for (int i = 0; i < (); i++)
{
((i));
if (!((i)))
{
    return false;
    }
}
return true;
}

3.使用

;

boolean isNunicodeDigits=("aaa123456789");


/commons/lang/api-release/下面的解释:

public static boolean isNumeric(String str)Checks if the String contains only unicode digits. A decimal point is not a unicode digit and returns false.

null will return false. An empty String ("") will return true.

(null)   = false

("")     = true

(" ")   = false

("123") = true

("12 3") = false

("ab2c") = false

("12-3") = false

("12.3") = false