Skip to content
Tutorials Center
My learning

Topics

Go Reference · bytes, unicode and text

Go unicode.IsLetter(), IsDigit() and IsSpace()

Reference for Go unicode.IsLetter, IsDigit, IsNumber, IsSpace, IsUpper, IsLower and IsPunct: what each accepts, how they differ, and how to validate text.

The unicode package has a family of predicates, each taking a rune and returning a bool. IsLetter is true for letters in any script, IsDigit for decimal digits, IsSpace for white space, and IsUpper, IsLower, IsPunct, IsSymbol, IsControl and IsPrint cover the other common classes.

They are the right tool for validating or tokenizing text that is not limited to ASCII. A test like r >= 'a' && r <= 'z' misses é, ж and 世; unicode.IsLetter does not.

Go
unicode.IsLetter('é')  // true
unicode.IsDigit('7')   // true
unicode.IsSpace('\t')  // true

Try it

Go Testing sample characters

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Go Validating and splitting text

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Syntax

Go
func IsLetter(r rune) bool
func IsDigit(r rune) bool
func IsNumber(r rune) bool
func IsSpace(r rune) bool
func IsUpper(r rune) bool
func IsLower(r rune) bool
func IsPunct(r rune) bool
func IsSymbol(r rune) bool
func IsControl(r rune) bool
func IsPrint(r rune) bool
Function True for
IsLetter Category L: letters of any script (a, é, ж, 世). Not _, not digits.
IsDigit Category Nd: decimal digits in any script (7, ٣).
IsNumber Categories N: digits plus other numerals such as ½, ² and Roman numerals.
IsSpace Space, \t \n \v \f \r, U+0085, U+00A0 and other Unicode white space.
IsUpper, IsLower Upper-case and lower-case letters. Letters in scripts without case are neither.
IsPunct Category P: ! , . ; : ? - ( ) " ' and similar.
IsSymbol Category S: $ + < = > ^ | ~ and currency, math and emoji symbols.
IsControl Control characters such as \x00 to \x1f.
IsPrint Letters, marks, numbers, punctuation, symbols and the ASCII space; excludes other spaces and controls.

Returns: true or false. None of them panic or take errors; invalid runes simply return false.

Usage notes

  • IsPunct and IsSymbol differ. ! is punctuation, but $, +, < and ^ are symbols, so "strip punctuation" code often needs both.
  • _ is punctuation, not a letter. Identifier checks must allow it explicitly, as in the example.
  • Digits versus numbers. Use IsDigit to accept 0-9 and other decimal digits, but note that it also accepts non-ASCII digits. For strict ASCII use r >= '0' && r <= '9'.
  • Case-less scripts. IsUpper('世') and IsLower('世') are both false, so do not treat "not upper" as "lower".
  • Combine with string helpers. The predicates have the signature func(rune) bool that strings.FieldsFunc, TrimFunc, IndexFunc and Map expect.
  • Iterate by rune. for _, r := range s yields runes; indexing s[i] gives bytes, which breaks for non-ASCII text.