Logo

Programming-Idioms

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

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.

import std.ascii;
import std.algorithm;
bool b = s.all!isDigit;
std.algorithm.iteration;
std.ascii;
bool b = s.filter!(a => !isDigit(a)).empty;

The result of filter is a lazy range. We know that only digits are present if this range is empty.
B := (for all Char of S => Char in '0' .. '9');

New implementation...
< >
programming-idioms.org