Golang unicode.IsSpace() Function with Examples

Golang | unicode.IsSpace() Function: Here, we are going to learn about the IsSpace() function of the unicode package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 17, 2021

unicode.IsSpace()

The IsSpace() function is an inbuilt function of the unicode package which is used to check whether the given rune r is a space character as defined by Unicode's White Space property.

It accepts one parameter (r rune) and returns true if rune r is a space character; false, otherwise.

Syntax:

func IsSpace(r rune) bool

Parameter(s):

  • r : Rune type value to be checked whether it is a space character or not.

Return Value:

The return type of the unicode.IsSpace() function is a bool, it returns true if rune r is a space character; false, otherwise.

Example 1:

// Golang program to demonstrate the
// example of unicode.IsSpace() Function

package main

import (
	"fmt"
	"unicode"
)

func main() {
	fmt.Println("unicode.IsSpace(' '):",
		unicode.IsSpace(' '))
	fmt.Println("unicode.IsSpace('\\t'):",
		unicode.IsSpace('\t'))
	fmt.Println("unicode.IsSpace('\\v'):",
		unicode.IsSpace('\v'))
	fmt.Println("unicode.IsSpace('\\f'):",
		unicode.IsSpace('\f'))
	fmt.Println("unicode.IsSpace('\\r')",
		unicode.IsSpace('\r'))
	fmt.Println("unicode.IsSpace('G'):",
		unicode.IsSpace('G'))
	fmt.Println("unicode.IsSpace('\\a'):",
		unicode.IsSpace('\a'))
}

Output:

unicode.IsSpace(' '): true
unicode.IsSpace('\t'): true
unicode.IsSpace('\v'): true
unicode.IsSpace('\f'): true
unicode.IsSpace('\r') true
unicode.IsSpace('G'): false
unicode.IsSpace('\a'): false

Example 2:

// Golang program to demonstrate the
// example of unicode.IsSpace() Function

package main

import (
	"fmt"
	"unicode"
)

func main() {
	var r rune
	var result bool

	r = ' '
	result = unicode.IsSpace(r)
	if result == true {
		fmt.Printf("%c (%x) is a space character.\n", r, r)
	} else {
		fmt.Printf("%c (%x) is not a space character.\n", r, r)
	}

	r = 'A'
	result = unicode.IsSpace(r)
	if result == true {
		fmt.Printf("%c (%x) is a space character.\n", r, r)
	} else {
		fmt.Printf("%c (%x) is not a space character.\n", r, r)
	}

	r = '\t'
	result = unicode.IsSpace(r)
	if result == true {
		fmt.Printf("%c (%x) is a space character.\n", r, r)
	} else {
		fmt.Printf("%c (%x) is not a space character.\n", r, r)
	}
}

Output:

  (20) is a space character.
A (41) is not a space character.
	 (9) is a space character.

Golang unicode Package »





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.