Check If the Rune is a Unicode Punctuation Character or not in Golang

Rune is a superset of ASCII or it is an alias of int32. It holds all the characters available in the world’s writing system, including accents and other diacritical marks, control codes like tab and carriage return, and assigns each one a standard number. This standard number is known as a Unicode code point or rune in the Go language.
You are allowed to check the given rune is a Unicode punctuation character or not with the help of IsPunct() function. This function returns true if the given rune is a Unicode punctuation character, or return false if the given rune is not a Unicode punctuation character. This function is defined under Unicode package, so for accessing this method you need to import the Unicode package in your program.

Syntax:

func IsPunct(r rune) bool

The return type of this function is boolean. Let us discuss this concept with the help of given examples:



Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Go program to illustrate how to check the
// given rune is a Unicode punctuation
// character or not
package main
  
import (
    "fmt"
    "unicode"
)
  
// Main function
func main() {
  
    // Creating rune
    rune_1 := 'g'
    rune_2 := 'e'
    rune_3 := '!'
    rune_4 := ','
    rune_5 := 'S'
  
    // Checking the given rune is a Unicode 
    // punctuation character or not
    // Using IsPunct() function
    res_1 := unicode.IsPunct(rune_1)
    res_2 := unicode.IsPunct(rune_2)
    res_3 := unicode.IsPunct(rune_3)
    res_4 := unicode.IsPunct(rune_4)
    res_5 := unicode.IsPunct(rune_5)
  
    // Displaying results
    fmt.Println(res_1)
    fmt.Println(res_2)
    fmt.Println(res_3)
    fmt.Println(res_4)
    fmt.Println(res_5)
  
}

chevron_right


Output:

false
false
true
true
false

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Go program to illustrate how to check the
// given rune is a Unicode punctuation
// character or not
package main
  
import (
    "fmt"
    "unicode"
)
  
// Main function
func main() {
  
    // Creating a slice of rune
    val := []rune{'g', 'f', 'G', '#', ',', ':'}
  
    // Checking each element of the given slice
    // of the rune is a Unicode punctuation 
    // character or not
    // Using IsPunct() function
    for i := 0; i < len(val); i++ {
  
        if unicode.IsPunct(val[i]) == true {
  
            fmt.Println("It is a Unicode punctuation character")
              
        } else {
          
            fmt.Println("It is not a Unicode punctuation character")
        }
    }
}

chevron_right


Output:

It is not a Unicode punctuation character
It is not a Unicode punctuation character
It is not a Unicode punctuation character
It is a Unicode punctuation character
It is a Unicode punctuation character
It is a Unicode punctuation character


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.