Golang strings.Index Function

The strings.Index function in Go is used to find the first occurrence of a substring within a string. It returns the index of the first character of the substring if it is found; otherwise, it returns -1. This function is part of the strings package and is useful for searching and locating substrings within a larger string.

Syntax

func Index(s, substr string) int

Parameters:

  • s: The string in which to search.
  • substr: The substring to search for.

Returns:

  • int: The index of the first occurrence of the substring in the string, or -1 if the substring is not found.

Example Usage

Basic Example

This example demonstrates how to use strings.Index to find the position of a substring within a string.

package main

import (
	"fmt"
	"strings"
)

func main() {
	str := "Hello, world!"
	substr := "world"

	index := strings.Index(str, substr)

	if index != -1 {
		fmt.Printf("The substring \"%s\" is found at index %d.\n", substr, index)
	} else {
		fmt.Printf("The substring \"%s\" is not found.\n", substr)
	}
}

Output:

The substring "world" is found at index 7.

Explanation:

  • The strings.Index function searches for the substring "world" within the string "Hello, world!".
  • Since the substring is found, the function returns the index 7, which is the starting position of "world" in the string.

Substring Not Found

If the substring is not found within the string, strings.Index returns -1.

Example

package main

import (
	"fmt"
	"strings"
)

func main() {
	str := "Hello, world!"
	substr := "Go"

	index := strings.Index(str, substr)

	if index != -1 {
		fmt.Printf("The substring \"%s\" is found at index %d.\n", substr, index)
	} else {
		fmt.Printf("The substring \"%s\" is not found.\n", substr)
	}
}

Output:

The substring "Go" is not found.

Explanation:

  • The strings.Index function searches for the substring "Go" within the string "Hello, world!".
  • Since the substring is not found, the function returns -1, indicating that the substring does not exist within the string.

Searching for an Empty Substring

If you search for an empty substring, strings.Index will always return 0, because an empty substring is considered to be found at the start of the string.

Example

package main

import (
	"fmt"
	"strings"
)

func main() {
	str := "Hello, world!"
	substr := ""

	index := strings.Index(str, substr)

	fmt.Printf("The substring \"%s\" is found at index %d.\n", substr, index)
}

Output:

The substring "" is found at index 0.

Explanation:

  • The strings.Index function returns 0 when searching for an empty substring, as it is considered to be located at the very beginning of the string.

Case Sensitivity

The strings.Index function is case-sensitive. If the case of the substring does not match the case of the corresponding characters in the string, the function will return -1.

Example

package main

import (
	"fmt"
	"strings"
)

func main() {
	str := "Hello, World!"
	substr := "world"

	index := strings.Index(str, substr)

	if index != -1 {
		fmt.Printf("The substring \"%s\" is found at index %d.\n", substr, index)
	} else {
		fmt.Printf("The substring \"%s\" is not found.\n", substr)
	}
}

Output:

The substring "world" is not found.

Explanation:

  • The strings.Index function is case-sensitive, so it returns -1 when searching for "world" in "Hello, World!" because the cases do not match ("World" vs. "world").

Real-World Use Case: URL Parameter Extraction

Suppose you need to extract a parameter from a URL by finding its position and slicing the string accordingly. You can use strings.Index for this task.

Example: URL Parameter Extraction

package main

import (
	"fmt"
	"strings"
)

func extractParameter(url, param string) string {
	start := strings.Index(url, param+"=")
	if start == -1 {
		return ""
	}
	start += len(param) + 1
	end := strings.Index(url[start:], "&")
	if end == -1 {
		return url[start:]
	}
	return url[start : start+end]
}

func main() {
	url := "https://example.com/search?q=golang&sort=desc"
	param := "q"

	value := extractParameter(url, param)
	if value != "" {
		fmt.Printf("The value of the parameter \"%s\" is \"%s\".\n", param, value)
	} else {
		fmt.Printf("The parameter \"%s\" is not found in the URL.\n", param)
	}
}

Output:

The value of the parameter "q" is "golang".

Explanation:

  • The extractParameter function uses strings.Index to find the start of the parameter in the URL and extract its value.
  • The URL parameter "q" is found and its value "golang" is returned.

Conclusion

The strings.Index function in Go is used for finding the position of a substring within a string. It’s especially useful for tasks such as searching for specific keywords, parsing strings, or simply checking if a substring exists within a string. Keep in mind that the function is case-sensitive and returns -1 when the substring is not found.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top