Golang strings.ContainsRune() Function with Examples

Golang | strings.ContainsRune() Function: Here, we are going to learn about the ContainsRune() function of the strings package with its usages, syntax, and examples.
Submitted by IncludeHelp, on August 15, 2021

strings.ContainsRune()

The ContainsRune() function is an inbuilt function of strings package which is used to check whether the string contains the given Unicode code point, it accepts two parameters – the first parameter is the string in which we have to check the Unicode code point and the second parameter is the Unicode code point to be checked. It returns a Boolean value. The result will be true if the string contains the Unicode code point, false otherwise.

Syntax:

func ContainsRune(str string, r rune) bool

Parameter(s):

  • str : String in which we have to check the Unicode code point.
  • r : Unicode code point value to be checked.

Return Value:

The return type of ContainsRune() function is a bool, it returns true if str contains the r, false otherwise.

Example 1:

// Golang program to demonstrate the
// example of strings.ContainsRune() Function

package main

import (
	"fmt"
	"strings"
)

func main() {
	// 72 is the Unicode code point value for 'H' (Uppercase)
	// 119 is the Unicode code point value for 'w' (Lowercase)
	// 97 is the Unicode code point value for 'a' (Lowercase)

	fmt.Println(strings.ContainsRune("Hello, world!", 72))
	fmt.Println(strings.ContainsRune("Hello, world!", 119))
	fmt.Println(strings.ContainsRune("Hello, world!", 97))
}

Output:

true
true
false

Example 2:

// Golang program to demonstrate the
// example of strings.ContainsRune() Function

package main

import (
	"fmt"
	"strings"
)

func main() {
	var str1 string = "New Delhi, India"
	var str2 string = "Los Angeles, California"
	var rune_value rune

	rune_value = 119 // for Lowercase 'w'
	if strings.ContainsRune(str1, rune_value) == true {
		fmt.Printf("\"%s\" contains %d ('%c')\n", str1, rune_value, rune_value)
	} else {
		fmt.Printf("\"%s\" does not contain %d ('%c')\n", str1, rune_value, rune_value)
	}

	if strings.ContainsRune(str2, rune_value) == true {
		fmt.Printf("\"%s\" contains %d ('%c')\n", str2, rune_value, rune_value)
	} else {
		fmt.Printf("\"%s\" does not contain %d ('%c')\n", str2, rune_value, rune_value)
	}

	rune_value = 103 // for Lowercase 'g'
	if strings.ContainsRune(str1, rune_value) == true {
		fmt.Printf("\"%s\" contains %d ('%c')\n", str1, rune_value, rune_value)
	} else {
		fmt.Printf("\"%s\" does not contain %d ('%c')\n", str1, rune_value, rune_value)
	}

	if strings.ContainsRune(str2, rune_value) == true {
		fmt.Printf("\"%s\" contains %d ('%c')\n", str2, rune_value, rune_value)
	} else {
		fmt.Printf("\"%s\" does not contain %d ('%c')\n", str2, rune_value, rune_value)
	}
}

Output:

"New Delhi, India" contains 119 ('w')
"Los Angeles, California" does not contain 119 ('w')
"New Delhi, India" does not contain 103 ('g')
"Los Angeles, California" contains 103 ('g')

Golang strings Package Functions »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.