Idiom #137 Check if string contains only digits
Set the boolean b to true if the string s contains only characters in the range '0'..'9', false otherwise.
- Ada
- C
- C
- C++
- C#
- D
- D
- Dart
- Elixir
- Erlang
- Erlang
- Fortran
- Go
- Go
- Haskell
- JS
- Java
- Java
- Java
- Java
- Java
- Kotlin
- Kotlin
- Lisp
- Lua
- Obj-C
- PHP
- Pascal
- Perl
- Python
- Python
- Python
- Ruby
- Rust
- Rust
- Rust
- Scala
- Smalltalk
- VB
char b = 0;
int n = strlen(s);
for (int i = 0; i < n; i++) {
if (! (b = (s[i] >= '0' && s[i] <= '9')))
break;
}
bool b = true;
const int n = strlen(s);
for (int i = 0; i < n; ++i) {
if (!isdigit(s[i])) {
b = false;
break;
}
}
bool b = false;
if (! s.empty() && std::all_of(s.begin(), s.end(), [](char c){return std::isdigit(c);})) {
b = true;
}
s must be a std::string
boolean b = s.chars()
.allMatch(Character::isDigit);
if (s.length() == 0) b = false;
"... If the stream is empty then true is returned and the predicate is not evaluated."
String d = "0123456789";
int i, n = s.length();
boolean b = n != 0;
for (i = 0; i < n; ++i)
if (d.indexOf(s.charAt(i)) == -1) {
b = false;
break;
}
programming-idioms.org