ADVERTISEMENT
ADVERTISEMENT

Golang strings.IndexRune() Function with Examples

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

strings.IndexRune()

The IndexRune() function is an inbuilt function of strings package which is used to get the index of the first occurrence (instance) of the Unicode code point (a rune type). It accepts two parameters – string and a UTF-8 Byte character (or Unicode code point), and returns the index of the first instance of the character in the string, -1 otherwise.

Syntax:

func IndexRune(str string, r rune) int

Parameter(s):

  • str : String in which we have to check the UTF-8 Byte character i.e., Unicode code point.
  • r : UTF-8 Byte character or Unicode code point to be checked.

Return Value:

The return type of IndexRune() function is an int, it returns the index of the first instance of the character in the string, -1 otherwise.

Example 1:

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

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println("Checking by characters...")
	fmt.Println(strings.IndexRune("HELLO, World!", '!'))
	fmt.Println(strings.IndexRune("HELLO, WORLD!", 'W'))
	fmt.Println(strings.IndexRune("Hello, world!", 'o'))

	fmt.Println("Checking by Unicode code points...")
	fmt.Println(strings.IndexRune("HELLO, World!", 33))
	fmt.Println(strings.IndexRune("HELLO, WORLD!", 87))
	fmt.Println(strings.IndexRune("Hello, world!", 111))
}

Output:

Checking by characters...
12
7
4
Checking by Unicode code points...
12
7
4

Example 2:

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

package main

import (
	"fmt"
	"strings"
)

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

	rune_value = 119 // for Lowercase 'w'
	index = strings.IndexRune(str1, rune_value)
	if index >= 0 {
		fmt.Printf("\"%s\" contains %d ('%c') at Index %d\n", str1, rune_value, rune_value, index)
	} else {
		fmt.Printf("\"%s\" does not contain %d ('%c')\n", str1, rune_value, rune_value)
	}

	index = strings.IndexRune(str2, rune_value)
	if index >= 0 {
		fmt.Printf("\"%s\" contains %d ('%c') at Index %d\n", str2, rune_value, rune_value, index)
	} else {
		fmt.Printf("\"%s\" does not contain %d ('%c')\n", str2, rune_value, rune_value)
	}

	rune_value = 103 // for Lowercase 'g'
	index = strings.IndexRune(str1, rune_value)
	if index >= 0 {
		fmt.Printf("\"%s\" contains %d ('%c') at Index %d\n", str1, rune_value, rune_value, index)
	} else {
		fmt.Printf("\"%s\" does not contain %d ('%c')\n", str1, rune_value, rune_value)
	}

	index = strings.IndexRune(str2, rune_value)
	if index >= 0 {
		fmt.Printf("\"%s\" contains %d ('%c') at Index %d\n", str2, rune_value, rune_value, index)
	} else {
		fmt.Printf("\"%s\" does not contain %d ('%c')\n", str2, rune_value, rune_value)
	}
}

Output:

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

Golang strings Package Functions »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.