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


Go has built-in support for Unicode . In Go, a code point from Unicode is represented by the rune type. Evaluating a rune to see if it contains a space character or not is one of the frequent actions when working with strings. Several methods for determining whether a rune in Go is a space character or not will be covered in this article.

Using the Unicode Package

Many functions for working with Unicode characters are offered by the unicode package, which is part of the Go standard library. One of these functions is called IsSpace, which gives a result of true if the provided rune is a space character and false otherwise.

Example

package main

import (
   "fmt"
   "unicode"
)

func main() {
   r := ' '
   if unicode.IsSpace(r) {
      fmt.Println("The rune is a space character")
   } else {
      fmt.Println("The rune is not a space character")
   }
}

Output

The rune is a space character

In the example above, we check if the rune r is a space character or not using the unicode.IsSpace function. If the rune is a space character, the output will be "The rune is a space character", and if it is not a space character, the output will be "The rune is not a space character".

The unicode package provides several other functions that can be used to determine whether a rune belongs to a specific Unicode category. For example, the IsLetter function can be used to check if a rune is a letter, and the IsDigit function can be used to check if a rune is a digit.

Using the ASCII Range

In ASCII, the space character is represented by the integer value 32. We can use this fact to determine whether a rune is a space character or not.

Example

package main

import "fmt"

func main() {
   r := ' '
   if r == ' ' {
      fmt.Println("The rune is a space character")
   } else {
      fmt.Println("The rune is not a space character")
   }
}

Output

The rune is a space character

In the example above, we check whether the rune r is a space character or not by comparing it with the ASCII value of the space character, which is 32.

Using the Switch Statement

We can also use a switch statement in Go to determine whether a rune is a space character or not.

Example

package main

import "fmt"

func main() {
   r := ' '
   switch {
   case r == ' ':
      fmt.Println("The rune is a space character")
   default:
      fmt.Println("The rune is not a space character")
   }
}

Output

The rune is a space character

In the example above, we use a switch statement to determine whether the rune r is a space character or not. We use the case statement with the condition r == ' ' to check if the rune is a space character.

Using the Unicode Categories

In Unicode, a space character belongs to the "Zs" category. The unicode package provides the In function that can be used to check if a rune belongs to a specific Unicode category.

Example

package main

import (
   "fmt"
   "unicode"
)

func main() {
   // Define a sample rune
   r := ' '

   // Check if the rune belongs to the "Zs" Unicode category
   if unicode.In(r, unicode.Zs) {
      fmt.Println("The rune is a space character")
   } else {
      fmt.Println("The rune is not a space character")
   }
}

Output

The rune is a space character

In this example, we define a sample rune that represents a space character, and use the unicode.In function to check if the rune belongs to the "Zs" Unicode category, which includes space characters. We then print the result accordingly.

Here is the complete code example for checking if a rune is a space character or not in Go −

Example

package main

import (
   "fmt"
   "unicode"
)

func main() {
   // Define a sample string with space characters
   s := "hello   world"

   // Iterate over each rune in the string and check if it is a space character
   for _, r := range s {
      if unicode.IsSpace(r) {
         fmt.Printf("%q is a space character\n", r)
      } else {
         fmt.Printf("%q is not a space character\n", r)
      }
   }

   // Check if a specific rune is a space character
   r := ' '
   if unicode.IsSpace(r) {
      fmt.Printf("%q is a space character\n", r)
   } else {
      fmt.Printf("%q is not a space character\n", r)
   }
}

Output

'h' is not a space character
'e' is not a space character
'l' is not a space character
'l' is not a space character
'o' is not a space character
' ' is a space character
' ' is a space character
' ' is a space character
'w' is not a space character
'o' is not a space character
'r' is not a space character
'l' is not a space character
'd' is not a space character
' ' is a space character

In this example, we defined a sample string with space characters and iterated over each rune in the string. We used the unicode.IsSpace function to check if each rune is a space character or not, and print the result accordingly. We have seen how to check if a specific rune is a space character or not by using the IsSpace function with a single rune argument.

Conclusion

Go has a number of methods for figuring out whether a rune is a space character or not. The IsSpace function, which may be used to detect whether or not a rune is a space character, is one of several functions offered by the unicode package that can be used to see if a rune falls into a particular Unicode category. To determine whether a rune represents a space character or not, we can either utilise the ASCII range or the switch statement.

Space characters must be handled carefully when working with strings in Go since they can change how various string operations, such splitting and trimming, behave. You may make sure that your Go programmes handle space characters appropriately and deliver the desired outcomes by employing the strategies described in this article.

Updated on: 07-Apr-2023

301 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements