Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a stl or boost function to determine if a string is numeric?

Tags:

c++

stl

boost

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...]

like image 621
Inno Avatar asked Apr 07 '11 08:04

Inno


People also ask

How do you determine if a string is a number in C++?

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.

How do I know if a Std string is number?

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.

How do you check if a char is not a number in C++?

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.

Is string in STL C++?

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.


1 Answers

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
like image 110
Jian Hu Avatar answered Oct 23 '22 07:10

Jian Hu