How do you check if a string contains only digits

This language bar is your friend. Select your favorite languages!
Select your favorite languages :
  • C
  • C++
  • C#
  • Go
  • Java
  • JS
  • Obj-C
  • PHP
  • Python
  • Ruby
  • Rust
  • Or search :

Idiom #137 Check if string contains only digits

Set boolean b to true if string s contains only characters in range '0'..'9', false otherwise.

  • Perl
  • C
  • C
  • Clojure
  • C++
  • C#
  • D
  • D
  • Dart
  • Elixir
  • Erlang
  • Erlang
  • Fortran
  • Go
  • Go
  • Haskell
  • JS
  • Java
  • Lisp
  • Lua
  • Obj-C
  • PHP
  • Pascal
  • Python
  • Ruby
  • Rust
  • Rust
  • Rust
  • Scala
  • Smalltalk
  • VB
  • Perl
my $b = $s =~ /^\d*$/;
  • Doc
  • C
  • C
  • Clojure
  • C++
  • C#
  • D
  • D
  • Dart
  • Elixir
  • Erlang
  • Erlang
  • Fortran
  • Go
  • Go
  • Haskell
  • JS
  • Java
  • Lisp
  • Lua
  • Obj-C
  • PHP
  • Pascal
  • Python
  • Ruby
  • Rust
  • Rust
  • Rust
  • Scala
  • Smalltalk
  • VB
#include <ctype.h> #include <stdbool.h> #include <string.h>
bool b = true; const int n = strlen(s); for (int i = 0; i < n; ++i) { if (!isdigit(s[i])) { b = false; break; } }
  • Demo
#include <string.h>
char b = 0; int n = strlen(s); for (int i = 0; i < n; i++) { if (! (b = (s[i] >= '0' && s[i] <= '9'))) break; }
  • Demo
(every? #(Character/isDigit %) s)
  • Doc
#include <algorithm> #include <cctype> #include <string>
bool b = false; if (! s.empty() && std::all_of(s.begin(), s.end(), [](char c){return std::isdigit(c);})) { b = true; }
  • Demo
bool b = s.All(char.IsDigit);
std.algorithm.iteration; std.ascii;
bool b = s.filter!(a => !isDigit(a)).empty;
import std.ascii; import std.algorithm;
bool b = s.all!isDigit;
final b = RegExp(r'^[0-9]+$').hasMatch(s);
  • Demo
  • Doc
b = Regex.match?(~r{\A\d*\z}, s)
Str = "21334", [Ch || Ch <- Str, Ch < $0 orelse Ch > $9] == [].
{_,Rest} = string:to_integer(S), B = Rest == "".
  • Doc
b = .true. do i=1, len(s) if (s(i:i) < '0' .or. s(i:i) > '9') then b = .false. exit end if end do
b := true for _, c := range s { if c < '0' || c > '9' { b = false break } }
  • Demo
import "strings"
isNotDigit := func(c rune) bool { return c < '0' || c > '9' } b := strings.IndexFunc(s, isNotDigit) == -1
  • Demo
  • Doc
import Data.Char
b = all isDigit s
var b = /^[0-9]+$/.test(s);
  • Demo
  • Origin
boolean b = s.matches("[0-9]*");
  • Demo
  • Doc
  • Origin
(setf b (every #'digit-char-p s))
b = tonumber(s) ~= nil
  • Demo
  • Doc
@import Foundation;
id nodigit=[[NSCharacterSet characterSetWithRange:NSMakeRange('0',10)].invertedSet copy]; BOOL b=![s rangeOfCharacterFromSet:nodigit].length;
$b = preg_match('/\D/', $s) !== 1;
  • Demo
  • Doc
var S: String; C: Char; B: Boolean; for C in S do begin B := C in ['0'..'9']; if not B then Break; end;
b = s.isdigit()
  • Demo
  • Doc
b = s.count("^0-9").zero?
  • Demo
let b = s.chars().all(char::is_numeric);
  • Demo
  • Doc
let b = s.bytes().all(|c| c.is_ascii_digit());
  • Demo
  • Doc
let chars_are_numeric: Vec<bool> = s.chars() .map(|c|c.is_numeric()) .collect(); let b = !chars_are_numeric.contains(&false);
  • Demo
  • Doc
def onlyDigits(s: String) = s.forall(_.isDigit)
b := s allSatisfy: #isDigit
Dim x As String = "123456" Dim b As Boolean = IsNumeric(x)

Do you know the best way to do this in your language ? New implementation...
Idiom created by programming-idioms.org
History
  • View revisions
Related idioms
  • Remove all non-digits characters
Cheatsheets
Issues
  • Report a bug

Video

Advertising