Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check non-numeric characters in string

Tags:

java

regex

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

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.

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

like image 900
HarsH Avatar asked Sep 30 '11 07:09

HarsH


People also ask

How do I check if a string contains a non numeric character in Python?

The isnumeric() method returns True if all the characters are numeric (0-9), otherwise False. Exponents, like ² and ¾ are also considered to be numeric values. "-1" and "1.5" are NOT considered numeric values, because all the characters in the string must be numeric, and the - and the . are not.

Is a non numeric character?

Non-Alphanumeric characters are except alphanumeric characters. space, percent sign, underscore, pipe, colon, semicolon, etc are non-alphanumeric characters. They can be also categorized as punctuation characters, symbol characters, etc.

How do you remove non numeric characters from a string in C++?

Remove all non alphanumeric characters from a string in C++ The std::remove_if algorithm returns an iterator that indicates where the end should be, which can be passed to the std::erase function. Starting with C++20, consider using the std::erase_if function that is error-free wrapper over the erase-remove idiom.


2 Answers

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 :

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
}
like image 137
Sandro Munda Avatar answered Oct 26 '22 17:10

Sandro Munda


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 :

return yourNumber.matches("-?\\d+(.\\d+)?");
like image 44
Jean-Charles Avatar answered Oct 26 '22 16:10

Jean-Charles