strings.Contains() / HasPrefix() / HasSuffix()
Go's strings package provides a set of useful functions for searching and testing strings. You can search within strings using functions like strings.Contains(), strings.HasPrefix(), and strings.HasSuffix().
Syntax
import "strings" // Reports whether substr is within s. ok := strings.Contains(s, substr) // Reports whether s begins with prefix. ok := strings.HasPrefix(s, prefix) // Reports whether s ends with suffix. ok := strings.HasSuffix(s, suffix) // Counts the number of non-overlapping occurrences of substr in s. n := strings.Count(s, substr) // Returns the byte offset of the first occurrence of substr in s (-1 if not found). idx := strings.Index(s, substr) // Returns the byte offset of the last occurrence of substr in s (-1 if not found). idx := strings.LastIndex(s, substr)
Function List
| Function | Description |
|---|---|
| strings.Contains(s, substr) | Returns true if s contains substr. |
| strings.ContainsAny(s, chars) | Returns true if s contains any character in chars. |
| strings.ContainsRune(s, r) | Returns true if s contains the rune r. |
| strings.HasPrefix(s, prefix) | Returns true if s begins with prefix. |
| strings.HasSuffix(s, suffix) | Returns true if s ends with suffix. |
| strings.Count(s, substr) | Returns the number of non-overlapping occurrences of substr in s. |
| strings.Index(s, substr) | Returns the byte index of the first occurrence of substr in s (-1 if not found). |
| strings.LastIndex(s, substr) | Returns the byte index of the last occurrence of substr in s (-1 if not found). |
| strings.EqualFold(s, t) | Compares s and t, ignoring case. |
Sample Code
package main
import (
"fmt"
"strings"
)
func main() {
s := "Hello, Go Language! Go is wonderful."
// Contains: check if a substring exists
fmt.Println("Contains 'Go':", strings.Contains(s, "Go"))
fmt.Println("Contains 'Java':", strings.Contains(s, "Java"))
fmt.Println("Contains 'a' or 'e':", strings.ContainsAny(s, "ae"))
fmt.Println()
// HasPrefix / HasSuffix: check beginning and end
fmt.Println("Starts with 'Hello':", strings.HasPrefix(s, "Hello"))
fmt.Println("Starts with '!':", strings.HasPrefix(s, "!"))
fmt.Println("Ends with '.':", strings.HasSuffix(s, "."))
fmt.Println("Ends with 'Go':", strings.HasSuffix(s, "Go"))
fmt.Println()
// Count: number of occurrences
fmt.Println("Occurrences of 'Go':", strings.Count(s, "Go"))
fmt.Println("Occurrences of 'is':", strings.Count(s, "is"))
fmt.Println()
// Index / LastIndex: finding positions
fmt.Println("First position of 'Go':", strings.Index(s, "Go"))
fmt.Println("Last position of 'Go':", strings.LastIndex(s, "Go"))
fmt.Println("Position of 'Rust':", strings.Index(s, "Rust")) // -1 if not found
fmt.Println()
// EqualFold: case-insensitive comparison
fmt.Println("'go' equals 'GO' (case-insensitive):", strings.EqualFold("go", "GO"))
fmt.Println("'Hello' equals 'hello' (case-insensitive):", strings.EqualFold("Hello", "hello"))
}
Notes
Go strings are handled as sequences of UTF-8 bytes. As a result, strings.Index() returns a byte index, not a character (rune) index. When working with multibyte characters such as Japanese text, keep in mind that byte indexes and character indexes differ. Use strings.IndexRune() when you need to search by character (rune).
strings.Index() returns a byte offset. For example, if a character at byte position 3 is found, you can access it with a slice like s[3:], but that does not mean it is the 3rd character. To count characters, use utf8.RuneCountInString() instead of len().
For splitting, joining, and replacing strings, see strings.Split() / Join() / Replace(). For case conversion, see strings.ToUpper() / ToLower() / TrimSpace().
If you find any errors or copyright issues, please contact us.