Logo

Programming-Idioms

History of Idiom 137 > diff from v3 to v4

Edit summary for version 4 by programming-idioms.org:
New Go implementation by user [programming-idioms.org]

Version 3

2016-06-10, 21:27:57

Version 4

2016-06-10, 21:46:01

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.

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.

Code
b := true
for _, c := range s {
	if c < '0' || c > '9' {
		b = false
		break
	}
}
Comments bubble
c has type rune.
Demo URL
https://play.golang.org/p/S634ZoDhDB