Home » Golang String ContainsRune()

Golang String ContainsRune()

ContainsRune() Function in Golang strings package is used to check the given string contains the specified rune in it or not. It returns true if the Unicode code point r in rune are present in the given string else returns false.

ContainsRune() Syntax

The syntax for ContainsRune() function is:

func ContainsRune(s1 string, r rune) bool

ContainsRune() Parameters:

The ContainsRune() function take two parameters

s1 is the string
r is the s rune

ContainsRune() Return Value

The strigs package ContainsRune() function returns bool value.

  • Returns true – if the s1 string contains the Unicode code point r in it
  • Returns false – if s1 string doesn’t contains specified rune

Let’s understand the string package ContainsRune() method with examples.

strings.ContainsRune() function to check string contains rune in go

The following go program checks if a string contains the specified rune using the strings.ContainsRune() function.

package main

import (
	"fmt"
	"strings"
)

func main() {
	// Check if a string contains a specified Unicode code point rune

	fmt.Println(strings.ContainsRune("GolangSpot", 83))
	fmt.Println(strings.ContainsRune("RuneExample", 98))
	fmt.Println(strings.ContainsRune("GolangSpot!", 33))
}

The output of the above string contains specified rune in go are:

true
false
true

In the above program, the first output returns true as the string “GolangSpot” contains the Unicode code point 83 in it. The Unicode code point of S is 83.

The second output returns false as the string “RuneExample” doesn’t contain the specified rune 98 ( Unicode code Point of b is 98).

The third output returns true as the string “GolnagSpot!” contains the Unicode code point rune 33 in it. The Unicode point of ! is 33.

Cool Tip: How to use the string ContainsAny function in Golang!

Conclusion

I hope the above article on how to check if the string contains a specified Unicode code point rune in golang using the ContainsRune() method is helpful to you.

You can find more topics about the Golang tutorials on the GolangSpot Home page.

Leave a Comment