×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang strings.LastIndexByte() Function with Examples

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

strings.LastIndexByte()

The LastIndexByte() function is an inbuilt function of strings package which is used to get the index of the last occurrence (instance) of a character (byte) in string. It accepts two parameters – string and a character (byte) and returns the last index, or -1 if the byte is not present in the string.

Syntax

func LastIndexByte(str string, c byte) int

Parameters

  • str : String in which we have to check the last index of a byte.
  • chr : Character (byte) to be checked in the string.

Return Value

The return type of LastIndexByte() function is a int, it returns the index of the last occurrence (instance) of character (byte) in the string, or -1 if the character (byte) is not present in the string.

Example 1

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

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.LastIndexByte("Hello, world", 'l'))
	fmt.Println(strings.LastIndexByte("Hello, world", 'x'))
	fmt.Println(strings.LastIndexByte("Okay@123", '@'))
}

Output:

10
-1
4

Example 2

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

package main

import (
	"fmt"
	"strings"
)

func main() {
	var str string
	var chr byte
	var index int

	str = "Hello, world!"
	chr = 'l'

	index = strings.LastIndexByte(str, chr)
	if index >= 0 {
		fmt.Printf("Last Index of %c in %q is %d index.\n", chr, str, index)
	} else {
		fmt.Printf("%c does not found in %q.\n", chr, str)
	}

	str = "Hello, world!"
	chr = 'x'

	index = strings.LastIndexByte(str, chr)
	if index >= 0 {
		fmt.Printf("Last Index of %c in %q is %d index.\n", chr, str, index)
	} else {
		fmt.Printf("%c does not found in %q.\n", chr, str)
	}

	str = "Okay@123"
	chr = '@'

	index = strings.LastIndexByte(str, chr)
	if index >= 0 {
		fmt.Printf("Last Index of %c in %q is %d index.\n", chr, str, index)
	} else {
		fmt.Printf("%c does not found in %q.\n", chr, str)
	}
}

Output:

Last Index of l in "Hello, world!" is 10 index.
x does not found in "Hello, world!".
Last Index of @ in "Okay@123" is 4 index.

Golang strings Package Functions »




Comments and Discussions!

Load comments ↻





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