- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find the last index value of a string in Golang?
LastIndex() is a built-in function of strings package in Golang. This function is used to check the index of the last occurrence of a specified substring in a given original string. If the substring is found in the given string, then it returns its index position, starting from 0; otherwise it returns "-1".
Syntax
The syntax of LastIndex() is −
func LastIndex(str, substr string) int
Where,
- str is the string inside which we need to search, and
- substr is the substring that we want to search inside the str.
Example 1
Let us consider the following example −
package main import ( "fmt" "strings" ) func main() { // Initializing the Strings p := "Programming Language" q := "String Function" r := "Golang String Function" s := "1234512345" // Display the Strings fmt.Println("String 1:", p) fmt.Println("String 2:", q) fmt.Println("String 3:", r) fmt.Println("String 4:", s) // Using the LastIndex Function test1 := strings.LastIndex(p, "ge") test2 := strings.LastIndex(q, "C") test3 := strings.LastIndex(r, "ng") test4 := strings.LastIndex(s, "23") // Display the LastIndex Output fmt.Println("LastIndex of 'ge' in String 1:", test1) fmt.Println("LastIndex of 'C' in String 2:", test2) fmt.Println("LastIndex of 'ng' in String 3:", test3) fmt.Println("LastIndex of '23' in String 4:", test4) }
Output
It will generate the following output −
String 1: Programming Language String 2: String Function String 3: Golang String Function String 4: 1234512345 LastIndex of 'ge' in String 1: 18 LastIndex of 'C' in String 2: -1 LastIndex of 'ng' in String 3: 11 LastIndex of '23' in String 4: 6
Observe that LastIndex() is case-sensitive; hence it returns "-1" for test2.
Example 2
Let us take another example.
package main import ( "fmt" "strings" ) func main() { var x string var y string // Intializing the Strings x = "LastIndex" y = "LastIndex Function" // Display the Strings fmt.Println("String 1:", x) fmt.Println("String 2:", y) // See if y is found in x using LastIndex Function if strings.LastIndex(y, x) != -1 { fmt.Println("String 2 is found in String 1") } else { fmt.Println("String 2 is not found in String 1") } }
Output
It will generate the following output −
String 1: LastIndex String 2: LastIndex Function String 2 is found in String 1
- Related Articles
- How to find the Index of a string in Golang?
- How to find the index of rune in the string in Golang?
- Golang Program to update the ith index node value, when index is at the last index.
- How to get the last index of an occurrence of the specified value in a string in JavaScript?
- How to find index of last occurrence of a substring in a string in Python?
- Find last index of a character in a string in C++
- MySQL String Last Index Of in a URL?
- Golang Program to get the index of the substring in a string
- Get the index of last substring in a given string in MySQL?
- Golang program to delete the ith index node, when the index is the last index in the linked list.
- How to find the index of the last occurrence of repeated values in a vector in R?
- Golang Program to update the last node value in a linked list.
- Python Pandas - Insert a new index value at the first index from the last
- How to find the Arc Cosine of a given value in Golang?
- How to find the Arc Sine of a given value in Golang?

Advertisements