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


Developers may easily manipulate Unicode characters and symbols. Thanks to the extensive built-in functions and packages provided by the Go programming language (also known as Golang). Determining whether a rune (a Unicode code point) is a punctuation character or not is a common problem when working with strings. Commas, periods, exclamation points, and other symbols used for punctuation in many languages are examples of punctuation characters. This article will examine how to use Golang to determine whether a given rune is a Unicode punctuation character and will include sample code to show how it works.

Using the "unicode" Package

Golang provides the "unicode" package, which includes a variety of functions that allow developers to work with Unicode characters. To check whether a given rune is a Unicode punctuation character, we can use the "IsPunct" function provided by the "unicode" package. Here's an example code snippet that demonstrates how to use this function −

Example

package main

import (
   "fmt"
   "unicode"
)

func main() {
   r1 := '!'
   r2 := 'a'
   r3 := ',' // Chinese comma character

   fmt.Println(unicode.IsPunct(r1)) // true
   fmt.Println(unicode.IsPunct(r2)) // false
   fmt.Println(unicode.IsPunct(r3)) // true
}

Output

true
false
true

In this code, we import the "unicode" package and call the "IsPunct" function on three different runes: '!', 'a', and ',' (a Chinese comma character). The function returns "true" for the first and third runes, which are both punctuation characters, and "false" for the second rune, which is not.

Using Regular Expressions

Another way to check whether a rune is a Unicode punctuation character is to use regular expressions. Golang's built-in "regexp" package includes support for Unicode regular expressions, which can be used to match Unicode characters in strings. Here's an example code snippet that demonstrates how to use regular expressions to check for Unicode punctuation characters −

Example

package main

import (
   "fmt"
   "regexp"
)

func main() {
   r1 := '!'
   r2 := 'a'
   r3 := ',' // Chinese comma character
   
   re := regexp.MustCompile(`\p{P}`)

   fmt.Println(re.MatchString(string(r1))) // true
   fmt.Println(re.MatchString(string(r2))) // false
   fmt.Println(re.MatchString(string(r3))) // true
}

Output

true
false
true

In this code, we use the "regexp.MustCompile" function to create a regular expression pattern that matches any Unicode punctuation character. We then call the "MatchString" method on each of the three runes, using the "string" function to convert them to strings. The "MatchString" function returns "true" for the first and third runes, which are punctuation characters, and "false" for the second rune, which is not.

Conclusion

Determining if a given rune in Golang represents a Unicode punctuation character is a simple process that can be carried out using either regular expressions or the "unicode" package. While the "regexp" package enables programmers to use regular expressions to match Unicode punctuation characters in strings, the "unicode" package has a straightforward "IsPunct" function that returns true if the provided rune is a Unicode punctuation character. These tools enable Golang programmers to quickly and easily manage Unicode characters and symbols in their programmes.

Updated on: 07-Apr-2023

268 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements