我写的方法,感觉很粗糙.求标准!
public boolean checkNum( String checkNum )
{try{
if( (checkNum == null) || (checkNum.equals("")) )
return false;
else
{
if( Double.isNaN(Double.parseDouble( checkNum )) == false 
&& checkNum.indexOf(".") == -1 
&& checkNum.indexOf("-") == -1
)
true;
else 
false;
}
}
catch (NumberFormatException e)
{
return false;
}
}
另一个方法
/// <returns>返回true则表示传入的字符串可以转换为整数,否则不能转换为整数</returns>
        private bool IsNumeric(string strTemp)
        {
            // 判断传入字串的每个字符是否是数字
            foreach (char c in strTemp)
            {
                // 返回true,表示是整型,false表示不是整型
                if (!char.IsNumber(c))
                {
                    return false;
                }
            }            return true;
        }public static boolean isValidDigitString( String s )
    {
         if(null == s || s.trim().length()==0)
             return false;
         String rex = "^\\d{1,"+s.length()+"}$";
         return Pattern.matches(rex, s);
   }
能不能解释下         
String rex = "^\\d{1,"+s.length()+"}$";
return Pattern.matches(rex, s);
水平有限,理解不了.

解决方案 »

  1.   

    public static boolean isValidDigitString( String s )
    {
    if(null == s || s.trim().length()==0)
    return false;
    String rex = "^\\d{1,"+s.length()+"}$";
    return Pattern.matches(rex, s);
    }
    能不能解释下
    String rex = "^\\d{1,"+s.length()+"}$";
    return Pattern.matches(rex, s);
    上个帖子不是给你说了,是正则表达式的吗
      

  2.   

    String rex = "^\\d{1,"+s.length()+"}$";
    ^ 行的开头 
    $ 行的结尾 
    \\d 0-9的数字
    \\d{1,"+s.length()+"}  0-9的数字,至少 1 次,但是不超过 s.length() 次