Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
strings.IndexByte() Function in Golang
IndexByte() is an inbuilt function of strings package in Golang. This function returns the index of the first occurrence of a character in a given string. If the character is found, then it returns its index, starting from 0; else it returns "-1".
Syntax
func IndexByte(str string, chr byte) int
Where,
- str – It is the original string.
- chr – Character (byte) to be checked in the string.
Example 1
Let us consider the following example −
package main
import (
"fmt"
"strings"
)
func main() {
// Initializing the Strings
m := "IndexByte String Function"
n := "Golang IndexByte String Package"
// Display the Strings
fmt.Println("First String:", m)
fmt.Println("Second String:", n)
// Using the IndexByte Function
output1 := strings.IndexByte(m, 'g')
output2 := strings.IndexByte(m, 'r')
output3 := strings.IndexByte(n, 'E')
output4 := strings.IndexByte(n, '4')
// Display the IndexByte Output
fmt.Println("IndexByte of 'g' in the First String:", output1)
fmt.Println("IndexByte of 'r' in the First String:", output2)
fmt.Println("IndexByte of 'E' in the Second String:", output3)
fmt.Println("IndexByte of '4' in the Second String:", output4)
}
Output
On execution, it will generate the following output −
First String: IndexByte String Function Second String: Golang IndexByte String Package IndexByte of 'g' in the First String: 15 IndexByte of 'r' in the First String: 12 IndexByte of 'E' in the Second String: -1 IndexByte of '4' in the Second String: -1
Example 2
Let's take another example −
package main
import (
"fmt"
"strings"
)
func main() {
// Defining the Variables
var s string
var cbyte byte
var result int
// Intializing the Strings
s = "IndexByte String Function"
cbyte = 'B'
// Display the Input String
fmt.Println("Given String:", s)
// Using the IndexByte Function
result = strings.IndexByte(s, cbyte)
// Output of IndexByte
fmt.Println("IndexByte of 'B' in the Given String:", result)
}
Output
It will generate the following output −
Given String: IndexByte String Function IndexByte of 'B' in the Given String: 5
Advertisements