Logo

Programming-Idioms

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

Implementation edit is for fixing errors and enhancing with metadata. Please do not replace the code below with a different implementation.

Instead of changing the code of the snippet, consider creating another Ada implementation.

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
boolean b = s.matches("[0-9]*");
b := true
for _, c := range s {
	if c < '0' || c > '9' {
		b = false
		break
	}
}
import "strings"
isNotDigit := func(c rune) bool { return c < '0' || c > '9' }
b := strings.ContainsFunc(s, isNotDigit) 
var b = /^[0-9]+$/.test(s);
var
  S: String;
  C: Char;
  B: Boolean;

  for C in S do
  begin
    B := C in ['0'..'9'];
    if not B then Break;
  end;
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 = s.count("^0-9").zero?
b = s.isdigit()
b = tonumber(s) ~= nil
import Data.Char
b = all isDigit s
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);
my $b = $s =~ /^\d*$/;
$b = preg_match('/\D/', $s) !== 1;
bool b = s.All(char.IsDigit);
def onlyDigits(s: String) = s.forall(_.isDigit) 
b =  Regex.match?(~r{\A\d*\z}, s)
(setf b (every #'digit-char-p 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;
}
(def b (every? #(Character/isDigit %) s))
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
Dim x As String = "123456"
Dim b As Boolean = IsNumeric(x)
{_,Rest} = string:to_integer(S),
B = Rest == "".
final b = RegExp(r'^[0-9]+$').hasMatch(s);
@import Foundation;
id nodigit=[[NSCharacterSet characterSetWithRange:NSMakeRange('0',10)].invertedSet copy];
BOOL b=![s rangeOfCharacterFromSet:nodigit].length;
#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;
}
let b = s.bytes().all(|c| c.is_ascii_digit());
#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;
  }
}
Str = "21334",
[Ch || Ch <- Str, Ch < $0 orelse Ch > $9] == [].
b := s allSatisfy: #isDigit
val regex = Regex("[0-9]*")
val b = regex.matches(s)
fun String?.isOnlyDigits() = !this.isNullOrEmpty() && this.all { Character.isDigit(it) }
boolean b = s.matches("[0-9]+");