How to check the specified rune in Golang String?

In Go language, strings are different from other languages like Java, C++, Python, etc. It is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding.
In the Go strings, you are allowed to check the given string contain the specified rune in it using ContainsRune() function. This function returns true if the given string contains the specified rune in it, or this function returns false if the given string does not contain the specified rune. It is defined under the string package so, you have to import string package in your program for accessing ContainsRune function.

Syntax:

func ContainsRune(str string, r rune) bool

Here, str is the string and r is s rune. The return type of this function is bool. Let us discuss this concept with the help of the given examples:



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Go program to illustrate how to check
// the given string containing the rune
package main
  
import (
    "fmt"
    "strings"
)
  
func main() {
  
    // Creating and initializing a string
    // Using shorthand declaration
    string_1 := "Welcome to GeeksforGeeks"
    string_2 := "AppleAppleAppleAppleAppleApple"
    string_3 := "%G%E%E%K%S"
  
    // Creating and initializing rune
    var r1, r2, r3 rune
    r1 = 'R'
    r2 = 'p'
    r3 = 42
  
    // Check the given string 
    // containing the rune
    // Using ContainsRune function
    res1 := strings.ContainsRune(string_1, r1)
    res2 := strings.ContainsRune(string_2, r2)
    res3 := strings.ContainsRune(string_3, r3)
  
    // Display the results
    fmt.Printf("String 1: %s , Rune 1: %q , Present or Not: %t",
                                             string_1, r1, res1)
      
    fmt.Printf("\nString 2: %s , Rune 2: %q , Present or Not: %t",
                                               string_2, r2, res2)
      
    fmt.Printf("\nString 3: %s , Rune 3: %q , Present or Not: %t",
                                               string_3, r3, res3)
  
}

chevron_right


Output:

String 1: Welcome to GeeksforGeeks , Rune 1: 'R' , Present or Not: false
String 2: AppleAppleAppleAppleAppleApple , Rune 2: 'p' , Present or Not: true
String 3: %G%E%E%K%S , Rune 3: '*' , Present or Not: false

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Go program to illustrate how to check
// the given string containing the rune
package main
  
import (
    "fmt"
    "strings"
)
  
func main() {
  
    // Creating and Checking the given 
    // rune present in the given string
    // Using ContainsRune function
    res1 := strings.ContainsRune("****Welcome, to,"+
                           " GeeksforGeeks****", 60)
  
    res2 := strings.ContainsRune("Learning x how x to "+
                     "x trim x a x slice of bytes", 'r')
  
    res3 := strings.ContainsRune("Geeks,for,Geeks, Geek", 'G')
  
    // Display the results
    fmt.Println("Final Result:")
    fmt.Println("Result 1: ", res1)
    fmt.Println("Result 2: ", res2)
    fmt.Println("Result 3: ", res3)
  
}

chevron_right


Output:

Final Result:
Result 1:  false
Result 2:  true
Result 3:  true


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.