检查字符串中的非数字字符

时间:2022-06-24 09:57:55

I want to check whether the String contains only numeric characters or it contains alpha-numeric characters too.

我想检查String是否只包含数字字符,还是包含字母数字字符。

I have to implement this check in database transaction where about a hundred-thousand records are going to be fetched and passed through this check, so I need optimized performance answer.

我必须在数据库事务中实现这个检查,其中将获取大约十万条记录并通过此检查,因此我需要优化的性能答案。

Please help.

请帮忙。

Currently, I have implemented this through Try-Catch Block : Parsed the string in Integer in try block and checked for the NumberFormatException in the catch block. Please suggest if I'm wrong.

目前,我已经通过Try-Catch Block实现了这一点:在try块中解析Integer中的字符串并检查catch块中的NumberFormatException。如果我错了,请建议。

Thanks in advance.

提前致谢。

2 个解决方案

#1


22  

You can check this with a regex.

您可以使用正则表达式进行检查。

Suppose that (numeric values only):

假设(仅限数值):

String a = "493284835";
a.matches("^[0-9]+$"); // returns true

Suppose that (alphanumeric values only):

假设(仅限字母数字值):

String a = "dfdf4932fef84835fea";
a.matches("^([A-Za-z]|[0-9])+$"); // returns true

As Pangea said in the comments area :

正如Pangea在评论区域所说:

If the performance are critical, it's preferrable to compile the regex. See below for an example :

如果性能很关键,那么编译正则表达式是首选。请参阅下面的示例:

String a = "dfdf4932fef84835fea";
Pattern pattern = Pattern.compile("^([A-Za-z]|[0-9])+$");
Matcher matcher = pattern.matcher(a);

if (matcher.find()) {
    // it's ok
}

#2


5  

Just Googling, I found out this link

只是谷歌搜索,我发现了这个链接

 public boolean containsOnlyNumbers(String str) {        
        //It can't contain only numbers if it's null or empty...
        if (str == null || str.length() == 0)
            return false;

        for (int i = 0; i < str.length(); i++) {

            //If we find a non-digit character we return false.
            if (!Character.isDigit(str.charAt(i)))
                return false;
        }

        return true;
    }

Edit: A RegExp to check numeric should be :

编辑:要检查数字的RegExp应该是:

return yourNumber.matches("-?\\d+(.\\d+)?");

#1


22  

You can check this with a regex.

您可以使用正则表达式进行检查。

Suppose that (numeric values only):

假设(仅限数值):

String a = "493284835";
a.matches("^[0-9]+$"); // returns true

Suppose that (alphanumeric values only):

假设(仅限字母数字值):

String a = "dfdf4932fef84835fea";
a.matches("^([A-Za-z]|[0-9])+$"); // returns true

As Pangea said in the comments area :

正如Pangea在评论区域所说:

If the performance are critical, it's preferrable to compile the regex. See below for an example :

如果性能很关键,那么编译正则表达式是首选。请参阅下面的示例:

String a = "dfdf4932fef84835fea";
Pattern pattern = Pattern.compile("^([A-Za-z]|[0-9])+$");
Matcher matcher = pattern.matcher(a);

if (matcher.find()) {
    // it's ok
}

#2


5  

Just Googling, I found out this link

只是谷歌搜索,我发现了这个链接

 public boolean containsOnlyNumbers(String str) {        
        //It can't contain only numbers if it's null or empty...
        if (str == null || str.length() == 0)
            return false;

        for (int i = 0; i < str.length(); i++) {

            //If we find a non-digit character we return false.
            if (!Character.isDigit(str.charAt(i)))
                return false;
        }

        return true;
    }

Edit: A RegExp to check numeric should be :

编辑:要检查数字的RegExp应该是:

return yourNumber.matches("-?\\d+(.\\d+)?");