Introduction
Working with strings is a fundamental part of many programming tasks. Go provides a rich set of functions in the strings
package for manipulating and analyzing strings. In this chapter, you will learn some of the most commonly used string functions in Go, including functions for modifying, searching, and splitting strings.
Basic String Functions
Length of a String
To get the length of a string, you can use the built-in len
function.
Example:
package main
import (
"fmt"
)
func main() {
str := "Hello, World!"
fmt.Println("Length of string:", len(str)) // Output: 13
}
Concatenating Strings
To concatenate strings, you can use the +
operator or the strings.Join
function.
Example:
package main
import (
"fmt"
"strings"
)
func main() {
str1 := "Hello"
str2 := "World"
result := str1 + ", " + str2 + "!"
fmt.Println(result) // Output: Hello, World!
// Using strings.Join
strSlice := []string{"Hello", "World"}
result = strings.Join(strSlice, ", ")
fmt.Println(result) // Output: Hello, World
}
String Comparison
To compare strings, you can use the ==
operator or the strings.Compare
function.
Example:
package main
import (
"fmt"
"strings"
)
func main() {
str1 := "Hello"
str2 := "hello"
str3 := "Hello"
fmt.Println(str1 == str2) // Output: false
fmt.Println(str1 == str3) // Output: true
// Using strings.Compare
fmt.Println(strings.Compare(str1, str2)) // Output: -1
fmt.Println(strings.Compare(str1, str3)) // Output: 0
}
Modifying Strings
Changing Case
To change the case of a string, use strings.ToUpper
, strings.ToLower
, and strings.Title
.
Example:
package main
import (
"fmt"
"strings"
)
func main() {
str := "Hello, World!"
fmt.Println(strings.ToUpper(str)) // Output: HELLO, WORLD!
fmt.Println(strings.ToLower(str)) // Output: hello, world!
fmt.Println(strings.Title(str)) // Output: Hello, World!
}
Trimming Strings
To trim whitespace or other characters from a string, use strings.TrimSpace
, strings.Trim
, strings.TrimPrefix
, and strings.TrimSuffix
.
Example:
package main
import (
"fmt"
"strings"
)
func main() {
str := " Hello, World! "
fmt.Println(strings.TrimSpace(str)) // Output: "Hello, World!"
fmt.Println(strings.Trim(str, " ")) // Output: "Hello, World!"
fmt.Println(strings.TrimPrefix(str, " ")) // Output: "Hello, World! "
fmt.Println(strings.TrimSuffix(str, " ")) // Output: " Hello, World!"
}
Replacing Substrings
To replace substrings, use strings.Replace
and strings.ReplaceAll
.
Example:
package main
import (
"fmt"
"strings"
)
func main() {
str := "Hello, World! Hello, Go!"
fmt.Println(strings.Replace(str, "Hello", "Hi", 1)) // Output: Hi, World! Hello, Go!
fmt.Println(strings.ReplaceAll(str, "Hello", "Hi")) // Output: Hi, World! Hi, Go!
}
Searching Strings
Checking Prefix and Suffix
To check if a string starts or ends with a specific substring, use strings.HasPrefix
and strings.HasSuffix
.
Example:
package main
import (
"fmt"
"strings"
)
func main() {
str := "Hello, World!"
fmt.Println(strings.HasPrefix(str, "Hello")) // Output: true
fmt.Println(strings.HasSuffix(str, "World!")) // Output: true
}
Contains and Index
To check if a string contains a substring or find the position of a substring, use strings.Contains
and strings.Index
.
Example:
package main
import (
"fmt"
"strings"
)
func main() {
str := "Hello, World!"
fmt.Println(strings.Contains(str, "World")) // Output: true
fmt.Println(strings.Index(str, "World")) // Output: 7
fmt.Println(strings.Index(str, "Go")) // Output: -1 (not found)
}
Last Index
To find the last occurrence of a substring, use strings.LastIndex
.
Example:
package main
import (
"fmt"
"strings"
)
func main() {
str := "Hello, World! Hello, Go!"
fmt.Println(strings.LastIndex(str, "Hello")) // Output: 14
}
Splitting Strings
To split a string into a slice of substrings, use strings.Split
, strings.SplitN
, and strings.Fields
.
Example:
package main
import (
"fmt"
"strings"
)
func main() {
str := "Hello, World! Hello, Go!"
fmt.Println(strings.Split(str, " ")) // Output: [Hello, World! Hello, Go!]
fmt.Println(strings.SplitN(str, " ", 3)) // Output: [Hello, World! Hello, Go!]
str2 := "Hello\tWorld\nHello Go!"
fmt.Println(strings.Fields(str2)) // Output: [Hello World Hello Go!]
}
Joining Strings
To join a slice of strings into a single string, use strings.Join
.
Example:
package main
import (
"fmt"
"strings"
)
func main() {
strSlice := []string{"Hello", "World", "Go"}
fmt.Println(strings.Join(strSlice, ", ")) // Output: Hello, World, Go
}
Conclusion
Go’s strings
package provides a comprehensive set of functions for string manipulation. By understanding and using these functions, you can effectively handle various string operations in your Go programs, from basic modifications to complex searches and transformations.