How to check the specified rune in Golang String?


In Go, a string is a sequence of Unicode characters. Each character in a string is represented by a Unicode code point, which is a unique integer value assigned to each character in the Unicode standard. Runes in Go are simply an alias for the int32 type, used to represent a Unicode code point. In this article, we'll discuss how to check for a specified rune in a Golang string.

Checking for a specified rune in a Golang string is relatively simple. We can use the strings.IndexRune function to check if a specific rune is present in a given string.

Here is the syntax of the strings.IndexRune function −

func IndexRune(s string, r rune) int

The IndexRune function takes two arguments: s and r. s is the string in which we want to search for the specified rune, and r is the rune we want to find.

The function returns the index of the first occurrence of the specified rune in the given string. If the specified rune is not found in the string, the function returns -1.

Example

Here's an example code snippet to demonstrate the usage of the strings.IndexRune function −

package main

import (
   "fmt"
   "strings"
)

func main() {
   s := "Hello, World!"
   r := 'l'

   index := strings.IndexRune(s, r)

   if index == -1 {
      fmt.Printf("Rune '%c' not found in string '%s'\n", r, s)
   } else {
      fmt.Printf("Rune '%c' found at index %d in string '%s'\n", r, index, s)
   }
}

Output

Rune 'l' found at index 2 in string 'Hello, World!'

In this example, we have defined a string s and a rune r that we want to search for in the string. We then call the strings.IndexRune function with these two arguments and store the result in the index variable.

We then check if the value of index is -1, which indicates that the specified rune was not found in the string. If this is the case, we print a message indicating that the rune was not found. Otherwise, we print a message indicating the index at which the rune was found.

Example

Here is another example −

package main

import "fmt"

func runeExists(str string, r rune) bool {
   for _, c := range str {
      if c == r {
         return true
      }
   }
   return false
}

func main() {
   str := "hello, world!"
   r1 := 'o'
   r2 := 'z'
    
   if runeExists(str, r1) {
      fmt.Printf("The rune '%c' exists in the string '%s'\n", r1, str)
   } else {
      fmt.Printf("The rune '%c' does not exist in the string '%s'\n", r1, str)
   }
    
   if runeExists(str, r2) {
      fmt.Printf("The rune '%c' exists in the string '%s'\n", r2, str)
   } else {
      fmt.Printf("The rune '%c' does not exist in the string '%s'\n", r2, str)
   }
}

Output

The rune 'o' exists in the string 'hello, world!'
The rune 'z' does not exist in the string 'hello, world!'

In this example, we define a function called runeExists() that takes a string str and a rune r as input. The function then iterates over each rune in the string and compares it to the rune we are looking for. If a match is found, the function returns true, otherwise it returns false.

In the main() function, we define a string str and two rune variables r1 and r2. We then call the runeExists() function to check if the r1 and r2 exist in the string str. Depending on the result, we print a message indicating whether the rune exists in the string or not.

Conclusion

Checking for a specified rune in a Golang string is a simple task using the strings.IndexRune function. By following the syntax and example provided in this article, you can easily check for a specified rune in any Golang string.

Updated on: 20-Apr-2023

411 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements