This language bar is your friend. Select your favorite languages!

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.

var
  S: String;
  C: Char;
  B: Boolean;

  for C in S do
  begin
    B := C in ['0'..'9'];
    if not B then Break;
  end;
(every? #(Character/isDigit %) s)
#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;
}
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;
b =  Regex.match?(~r{\A\d*\z}, s)
{_,Rest} = string:to_integer(S),
B = Rest == "".
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
import "strings"
isNotDigit := func(c rune) bool { return c < '0' || c > '9' }
b := strings.IndexFunc(s, isNotDigit) == -1
b := true
for _, c := range s {
	if c < '0' || c > '9' {
		b = false
		break
	}
}
import Data.Char
b = all isDigit s
var b = /^[0-9]+$/.test(s);
boolean b = s.matches("[0-9]*");
(setf b (every #'digit-char-p s))
b = tonumber(s) ~= nil
$b = preg_match('/\D/', $s) !== 1;
my $b = $s =~ /^\d*$/;
b = s.isdigit()
b = s.count("^0-9").zero?
let chars_are_numeric: Vec<bool> = s.chars()
				.map(|c|c.is_numeric())
				.collect();
let b = !chars_are_numeric.contains(&false);
let b = s.chars().all(char::is_numeric);
def onlyDigits(s: String) = s.forall(_.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