How to check the specified rune in Golang String?
Last Updated :
05 Sep, 2019
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:
C
// 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)
}
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:
C
// 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)
}
Output:
Final Result:
Result 1: false
Result 2: true
Result 3: true
Similar Reads
How to Get the String in Specified Base in Golang? Go language provides inbuilt support to implement conversions to and from string representations of basic data types by strconv Package. This package provides a FormatInt() function which is used to return the string representation of x in the given base, i.e., 2 <= base <= 36. Here, the resul
2 min read
How to Map a Rune to the Specified Case 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 c
2 min read
How to Replace Characters 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 Go strings, you are allowed to replace characters in the given string usin
4 min read
How to Split a String in Golang? In Go language, strings differ from other languages like Java, C++, and Python. A string in Go is a sequence of variable-width characters, with each character represented by one or more bytes using UTF-8 encoding. In Go, you can split a string into a slice using several functions provided in the str
3 min read
Golang | How to find the index of rune in the 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 can also find the first index of the specified rune in
3 min read