Logo

Programming-Idioms

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

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.

b := true
for _, c := range s {
	if c < '0' || c > '9' {
		b = false
		break
	}
}

c has type rune.
import "strings"
isNotDigit := func(c rune) bool { return c < '0' || c > '9' }
b := strings.ContainsFunc(s, isNotDigit) 
B := (for all Char of S => Char in '0' .. '9');

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