Checking if a string contains an integer

check if string contains at least one number java
java check if string contains numbers
java check if string contains letters and numbers
how to check if a string contains only numbers in java
check if string contains int python
java check if string is positive integer
check if string contains non-numeric characters java
how to check if a number contains a certain digit in java

Do you know of a function that can check if a string contains an integer?

Here's how I'd expect it to work:

holds_int("23") // should return true.  
holds_int("2.3") // should return false.  
holds_int("qwe") // should return false.
if((string)(int)$var == $var) {
    echo 'var is an integer or a string representation of an integer';
}

Example results:

var_dump( test(1)             ); // TRUE
var_dump( test('1')           ); // TRUE
var_dump( test('1.0')         ); // TRUE
var_dump( test('1.1')         ); // false
var_dump( test('0xFF')        ); // false
var_dump( test('0123')        ); // TRUE
var_dump( test('01090')       ); // TRUE
var_dump( test('-1000000')    ); // TRUE
var_dump( test('+1000000')    ); // TRUE
var_dump( test('2147483648')  ); // false
var_dump( test('-2147483649') ); // false

See Gordon's answer below for how this would behave differently if === were used for comparison instead of ==.

Check if a String contains numbers Java, So I needed to check if the search string contained at least one number. Simple regex did the job. public boolean stringContainsNumber( String s )  The Integer class has a number of static methods for parsing strings. For the case where we want to check if if a string contains a valid integer we can use the method Integer.parseInt() and catch the exception that is thrown when the number cannot be parsed.

Not the fastest method, but filter_var() is quite accurate:

function test($s)
{
    return filter_var($s, FILTER_VALIDATE_INT) !== false;
}

Here are the results based on Jhong's answer, differences marked with !!:

var_dump(test(1)            ); // true
var_dump(test('1')          ); // true
var_dump(test('1.0')        ); // false !!
var_dump(test('1.1')        ); // false
var_dump(test('0xFF')       ); // false
var_dump(test('0123')       ); // false !!
var_dump(test('01090')      ); // false !!
var_dump(test('-1000000')   ); // true
var_dump(test('+1000000')   ); // true
var_dump(test('2147483648') ); // true !! on 64bit
var_dump(test('-2147483649')); // true !! on 64bit

To allow octal integers:

function test($s)
{
   return filter_var($s, FILTER_VALIDATE_INT, FILTER_FLAG_ALLOW_OCTAL) !== false;
}

Results:

var_dump(test('0123') ); // true
var_dump(test('01090')); // false !!

To allow hexadecimal notation:

function test($s)
{
   return filter_var($s, FILTER_VALIDATE_INT, FILTER_FLAG_ALLOW_HEX) !== false;
}

Results:

var_dump(test('0xFF')); // true !!

Check if a string contains a number using Java, If you want to check whether a Python string is an integer, you can try casting to an int with int() and catching the ValueError if it's not an integer: To check for nonnegative integers, you can use the str.is_digit() method. It will "return true if all characters in the string are digits and there is at least one character, false otherwise: It will "return true if all characters in the string are digits and there is at least one character, false otherwise:

Dont want to accidently turn Jhong's answer into a CW, so for the record here is the results when testing with === instead of ==.

function test($var) {
    return ((string)(int)$var === $var);
}

var_dump( test(1)             ); // returns false vs TRUE
var_dump( test('1')           ); // returns TRUE
var_dump( test('1.0')         ); // returns false vs TRUE
var_dump( test('1.1')         ); // returns false 
var_dump( test('0xFF')        ); // returns false
var_dump( test('0123')        ); // returns false vs TRUE
var_dump( test('-0123')       ); // returns false vs TRUE
var_dump( test('-1000000')    ); // returns TRUE
var_dump( test('+1000000')    ); // returns false vs TRUE
var_dump( test('2147483648')  ); // returns false
var_dump( test('-2147483649') ); // returns false

Testing whether a Python string contains an integer, To find whether a given string contains a number, convert it to a character array and find whether each character in the array is a digit using the  How To Check If String Contains Numeric Number (SQL Server) Yesterday I was working on a project where we need to exclude those records where column contains numeric number in value even if its a single numeric digit. So I googled a bit and found a very easy trick. Using simple regular expression

Update Since PHP 7.1 there are problems with using is_int() with non-numeric values, as discussed in this SO Answer. In any case, this is a very old answer and I'd really view it as something of a hack at this point so YMMV ;)

Sorry if this question has been answered but this has worked for me in the past:

First check if the string is_numeric. if it is add a 0 to the value to get PHP to covert the string to its relevant type. Then you can check if it's an int with is_int. Quick and dirty but it works for me...

$values = array(1, '2', '2.5', 'foo', '0xFF', 0xCC, 0644, '0777');

foreach ($values as $value) {
  $result = is_numeric($value) && is_int(($value + 0)) ? 'true' : 'false';
  echo $value . ': ' . $result . '<br />';
}

Results:

1: true
2: true
2.5: false
foo: false
0xFF: true
204: true
420: true
0777: true

The only problem is that it will evaluate octal values wrapped in a string literally, i.e: '0123' will simply become 123. But that's easy to address :)

Check if a string contains a number using Java., To check whether the above string has only digit characters, try the following if condition that uses matches() method and checks for every  str: It is the string which is to be checked. Type of this parameter is System.String. Return Value: It returns the boolean value. If substring exists in string or value is the empty string (“”), then it returns True, otherwise returns False.

Other option

function holds_int($str)
   {
   return preg_match("/^-?[0-9]+$/", $str);
   }

Check whether the String contains only digit characters in Java, It contains well written, well thought and well explained computer science and Write a function to check whether a given input is an integer or a string. Check if a string contains a number using Java. To find whether a given string contains a number, convert it to a character array and find whether each character in the array is a digit using the isDigit() method of the Character class.

Program to check if input is an integer or a string, Given a string, write a Python program to find whether a string contains only numbers or not. Let's see a few methods to solve the above task. Method #1: Using  The contains() method is Java method to check if String contains another substring or not. It returns boolean value so it can use directly inside if statements. This method returns true only if this string contains "s" else false. NullPointerException − if the value of s is null.

Python, Explore different ways to determine whether a String is numeric or not. but this method can also be modified to check for Integer, Float, Long and large MAX_VALUE); 3 contain numbers but are still malformatted — 'x0',  Say you have a string that you want to test to make sure that it contains an integer before you proceed with other the rest of the code. What would you use, in java, to find out whether or not it i

Check If a String Is Numeric in Java, To check if string contains numbers only, in the try block, we use Double 's parseDouble() method to convert the string to a Double . If it throws an error (i.e.  1) Check if a string contains a number using the Double wrapper class Use the parseDouble method of the Double wrapper class to check. If the string does not contain a number, the parseDobule method throws NumberFormatException exception which you can catch to do further processing.

Comments
  • (related) Check whether the string is a unix timestamp - the test is basically the same
  • @mathk: I need to validate user input.
  • And what input exactly is considered valid?
  • Do you consider '1.0', '0xFF' or '0123' (octal) as integers for the test and what if you pass in an integer instead of a string?
  • @Gumbo: Any natural number would be considered valid input in the case I'm working on.
  • this should test with === and make sure the number is inside the integer range
  • === won't work if $var actually is an integer. == will, regardless of whether it is a string or an int. OP didn't ask for that specifically, but it seems a bit perverse to return false if it actually is an integer.
  • @Gordon: Casting to integer will always yield a valid integer.
  • I'd rather do something like (string)(int)$var === (string)$var, to eliminate what I see as a false positive in your list, 1.0.
  • @nyson, you should make that an answer so I could vote for it.
  • I just used your function and it has problems with large numbers. Given the fact that the requirement is to check if a string contains an integer, but doesn't say how long can it be, I believe that a regular expression approach might be better in this case. For a number like 999999999999999999 it will work, but if you try for instance with a string containing the number 9999999999999999999, which is a valid integer, it will fail.