Check If the Rune is a Symbolic Character or not in Golang


A variety of built-in functions and packages are available in the Go programming language (also known as Golang), which makes it simple to work with characters and symbols in strings. Verifying a rune's (a Unicode code point) status as a symbolic character is a frequent operation when working with strings. Punctuation marks, mathematical symbols, and other non-alphanumeric characters can all be considered symbolic.

In this article, we will explore how to check whether a given rune is a symbolic character or not using Golang, and we'll provide some sample code to demonstrate how it works.

Using the unicode.IsSymbol() Function

Go provides the IsSymbol() function in the unicode package to check if a rune is a symbolic character or not. This function returns true if the given rune is a symbolic character; otherwise, it returns false.

Here is an example program that demonstrates the use of the IsSymbol() function −

Example

package main

import (
   "fmt"
   "unicode"
)

func main() {
   r1 := '@'
   r2 := 'A'
   r3 := '1'
   r4 := '★'
    
   fmt.Println(unicode.IsSymbol(r1)) // true
   fmt.Println(unicode.IsSymbol(r2)) // false
   fmt.Println(unicode.IsSymbol(r3)) // false
   fmt.Println(unicode.IsSymbol(r4)) // true
}

In this program, we import the unicode package and use the IsSymbol() function to check if the given runes are symbolic characters or not. We pass the runes @, A, 1, and ★ to the function and print the results.

The output of this program is as follows −

Output

false
false
false
true

As we can see from the output, the IsSymbol() function correctly identifies the symbolic characters in the given runes.

Using the unicode.IsGraphic() Function

Go also provides the IsGraphic() function in the unicode package to check if a rune is a visible character or not. This function returns true if the given rune is a visible character; otherwise, it returns false.

Here is an example program that demonstrates the use of the IsGraphic() function −

Example

package main

import (
   "fmt"
   "unicode"
)

func main() {
   r1 := '@'
   r2 := 'A'
   r3 := '1'
   r4 := '★'
    
   fmt.Println(unicode.IsGraphic(r1)) // true
   fmt.Println(unicode.IsGraphic(r2)) // true
   fmt.Println(unicode.IsGraphic(r3)) // true
   fmt.Println(unicode.IsGraphic(r4)) // true
}

In this program, we use the IsGraphic() function to check if the given runes are visible characters or not. We pass the runes @, A, 1, and ★ to the function and print the results.

The output of this program is as follows −

Output

true
true
true
true

As we can see from the output, the IsGraphic() function correctly identifies all the given runes as visible characters.

Conclusion

In Go, we can use the unicode.IsSymbol() function to check if a rune is a symbolic character or not. We can also use the unicode.IsGraphic() function to check if a rune is a visible character or not. These functions are useful when working with Unicode characters in Go.

Updated on: 07-Apr-2023

432 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements