strings.Index, LastIndex Funcs
This page was last reviewed on Nov 30, 2021.
Dot Net Perls
Index, LastIndex. A string contains an important substring. With Index, we search for that substring. And with LastIndex we search from right to left.
Common functionality. A for-loop can be used to iterate through, and test, a string. But with Index and LastIndex this is done in a simpler way, in a single func call.
First example. We import the "strings" package. Then we assign 2 strings—value1 is part of value2. Index returns the integer index of value1.
Return If the substring is not found within the string, Index returns -1. The search goes from left to right.
Result This program prints the value "FOUND" because the string "rainbow" is found within "the rainbow."
package main import ( "fmt" "strings" ) func main() { value1 := "rainbow" value2 := "the rainbow" // See if value1 is found in value2. if strings.Index(value2, value1) != -1 { fmt.Println("FOUND") } }
Index result. Index returns an int index—this is the position where the string is found. Remember strings are indexed starting at zero.
package main import ( "fmt" "strings" ) func main() { value := "cat dog" // Get index of this substring. result := strings.Index(value, "dog") fmt.Println(result) }
LastIndex. This searches from right to left. It begins its search at the last index and proceeds to the first index. So it finds rightmost matches first.
Here The index of the second substring "one," at 8, is located, not the first substring at index 0.
package main import ( "fmt" "strings" ) func main() { input := "one two one" // Get last index, searching from right to left. i := strings.LastIndex(input, "one") fmt.Println(i) }
IndexFunc. This method receives a func argument that indicates what characters are matched. In the func, any Go code can be used. When true is returned, the rune is matched.
Detail The func passed to IndexFunc can be specified as a local variable. It could be specified directly as an argument.
Further Any func that receives a rune and returns a bool can be used. The func could use a map or external data source to test chars.
package main import ( "fmt" "strings" ) func main() { f := func(c rune) bool { // Return true if colon or space char. return c == ':' || c == ' '; } value := "one:two" // Pass func to method. result := strings.IndexFunc(value, f) fmt.Println(result) value = "one two" result = strings.IndexFunc(value, f) fmt.Println(result) }
3 3
IndexAny. This method receives a string that represents a set of characters. The first index of any character in that set is returned.
package main import ( "fmt" "strings" ) func main() { value := "bird" // Finds the letter "i" index. // ... No "a" is found in the string. result1 := strings.IndexAny(value, "ai") fmt.Println("RESULT1:", result1, value[result1:result1+1]) // Find index of first character in the set of chars. result2 := strings.IndexAny(value, "x?rd_") fmt.Println("RESULT2:", result2, value[result2:result2+1]) }
RESULT1: 1 i RESULT2: 2 r
Between, before and after. We can extract parts of a string based on the surrounding substrings. With strings.Index and LastIndex we locate a substring.
String Between
A summary. Character searching as common task in programs. If more complex tests are needed, a for-loop can be used, but for simple searches, Index and LastIndex are ideal.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 30, 2021 (edit link).
© 2007-2024 Sam Allen.