Golang | How to find the index of rune in the string?
Last Updated :
28 Aug, 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 can also find the first index of the specified rune in the given string using
IndexRune() function. This function returns the index of the first instance of the Unicode code point, i.e, specified rune, or -1 if the specified rune is not present in the given string. If the rune is
utf8.RuneError, then it returns the first instance of any invalid UTF-8 byte sequence. It is defined under the string package so, you have to import string package in your program for accessing IndexRune function.
Syntax:
func IndexRune(str string, r rune) int
Example 1:
C
// Go program to illustrate how to find
// the index value of the given rune
package main
import (
"fmt"
"strings"
)
func main() {
// Creating and Finding the first index
// of the rune in the given string
// Using IndexRunefunction
res1 := strings.IndexRune("****Welcome to GeeksforGeeks****", 60)
res2 := strings.IndexRune("Learning how to trim"+
" a slice of bytes", 'r')
res3 := strings.IndexRune("GeeksforGeeks", 'G')
// Display the results
fmt.Println("Index Value 1: ", res1)
fmt.Println("Index Value 2: ", res2)
fmt.Println("Index Value 3: ", res3)
}
Output:
Index Value 1: -1
Index Value 2: 3
Index Value 3: 0
Example 2:
C
// Go program to illustrate how to find
// the index value of the given 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 = 'l'
r3 = 42
// Finding the first index
// of the given rune
// Using IndexRune function
res1 := strings.IndexRune(string_1, r1)
res2 := strings.IndexRune(string_2, r2)
res3 := strings.IndexRune(string_3, r3)
// Display the results
fmt.Printf("String 1: %s , Rune 1:%q , Index Value: %d",
string_1, r1, res1)
fmt.Printf("\nString 2: %s , Rune 2:%q , Index Value: %d",
string_2, r2, res2)
fmt.Printf("\nString 3: %s , Rune 3:%q , Index Value: %d",
string_3, r3, res3)
}
Output:
String 1: Welcome to GeeksforGeeks , Rune 1:'R' , Index Value: -1
String 2: AppleAppleAppleAppleAppleApple , Rune 2:'l' , Index Value: 3
String 3: %G%E%E%K%S , Rune 3:'*' , Index Value: -1
Similar Reads
How to find the length of the pointer in Golang? Pointers in Go programming language or Golang is a variable which is used to store the memory address of another variable. Pointers in Golang are also termed as the special variables. The variables are used to store some data at a particular memory address in the system. The memory address is always
2 min read
How to find the type of Struct in Golang? A structure or struct in Golang is a user-defined data type which is a composition of various data fields. Each data field has its own data type, which can be a built-in or another user-defined type. Struct represents any real-world entity that has some set of properties/fields. Go does not support
3 min read
How to Read a File Line by Line to String in Golang? To read a file line by line the bufio package Scanner is used. Let the text file be named as sample.txt and the content inside the file is as follows: GO Language is a statically compiled programming language, It is an open-source language. It was designed at Google by Rob Pike, Ken Thompson, and Ro
2 min read
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 spec
3 min read
How to find the last index value in slice of bytes in Golang? In Go, a slice is a flexible data structure that stores a variable-length sequence of elements of the same type. The LastIndex() function from the bytes package returns the last index of a specified value in a byte slice, or -1 if the value is not found.Examplepackage mainimport ( "bytes" "fmt")func
3 min read