TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Golang Index, LastIndex: strings Funcs

These Go example programs use the Index, LastIndex and IndexAny funcs. They search strings for substrings.

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.

An example. First we import the "strings" package. Then we assign two 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."

Based on:

Golang 1.4

Golang program that uses index, strings

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")
    }
}

Output

FOUND

Index result. Index returns an int index—this is the position where the string is found. Remember strings are indexed starting at zero.

Golang program that uses Index, displays result

package main

import (
    "fmt"
    "strings"
)

func main() {
    value := "cat dog"

    // Get index of this substring.
    result := strings.Index(value, "dog")
    fmt.Println(result)
}

Output

4

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.

Golang program that uses LastIndex

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)
}

Output

8

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.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf