I'm very new to C++, boost etc.
I would like to know if there is already a function in boost or STL I can use to determine if a string is numeric.
Numeric strings may look like: 100
or
100.52
I know there are tons of examples how to write such a function but I would like to know if there is already a function I can use for this.
I'm looking for a pure C++-solution, not C.
[UPDATE: I'm already using lexical_cast to convert my strings, I'm just wondering if there is a method like is_numeric I can use for this...]
Using built-in method isdigit(), each character of string is checked. If the string character is a number, it will print that string contains int. If string contains character or alphabet, it will print that string does not contain int.
Use std::isdigit Method to Determine if a String Is a Number Namely, pass a string as a parameter to a function isNumber , which iterates over every single char in the string and checks with isdigit method. When it finds the first non-number the function returns false, if none is found returns true.
The function isdigit() is used to check that character is a numeric character or not. This function is declared in “ctype. h” header file. It returns an integer value, if the argument is a digit otherwise, it returns zero.
The actual HP/SGI STL, the original library, parts of which the C++ Standard Library was based on. They included containers, iterators and algorithms. Strings were not a part of this. The parts of the C++ Standard Library that were based on the SGI STL library: containers, iterators and algorithms.
The following code
The following sentense, return true if "str" is composed of 0~9 only, otherwise, return false.
return str.find_first_not_of("0123456789") == std::string::npos
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With