strings.ToUpper() / ToLower() / TrimSpace()
The strings package in Go provides functions for converting string case and trimming (removing leading/trailing characters). They are commonly used for normalizing user input and performing case-insensitive comparisons.
Syntax
import "strings" // Converts to uppercase. upper := strings.ToUpper(s) // Converts to lowercase. lower := strings.ToLower(s) // Converts to title case (capitalizes the first letter of each word). title := strings.Title(s) // Deprecated. Use golang.org/x/text/cases instead. // Removes leading and trailing whitespace. trimmed := strings.TrimSpace(s) // Removes leading and trailing characters found in the cutset. trimmed := strings.Trim(s, cutset) // Removes leading characters only. trimmed := strings.TrimLeft(s, cutset) // Removes trailing characters only. trimmed := strings.TrimRight(s, cutset) // Removes a leading substring (exact match). trimmed := strings.TrimPrefix(s, prefix) // Removes a trailing substring (exact match). trimmed := strings.TrimSuffix(s, suffix)
Function Reference
| Function | Description |
|---|---|
| strings.ToUpper(s) | Returns a copy of s with all characters converted to uppercase. |
| strings.ToLower(s) | Returns a copy of s with all characters converted to lowercase. |
| strings.TrimSpace(s) | Removes leading and trailing whitespace (spaces, tabs, newlines, etc.). |
| strings.Trim(s, cutset) | Removes all leading and trailing characters contained in cutset. |
| strings.TrimLeft(s, cutset) | Removes leading characters contained in cutset. |
| strings.TrimRight(s, cutset) | Removes trailing characters contained in cutset. |
| strings.TrimPrefix(s, prefix) | Returns s with the leading prefix removed, if present. |
| strings.TrimSuffix(s, suffix) | Returns s with the trailing suffix removed, if present. |
| strings.TrimFunc(s, f) | Removes leading and trailing characters for which function f returns true. |
| strings.Repeat(s, n) | Returns a new string consisting of s repeated n times. |
Sample Code
package main
import (
"fmt"
"strings"
)
func main() {
// ToUpper / ToLower: case conversion
s := "Hello, Go!"
fmt.Println("ToUpper:", strings.ToUpper(s))
fmt.Println("ToLower:", strings.ToLower(s))
// Case-insensitive comparison
a, b := "Go", "go"
fmt.Println("Case-insensitive equal:", strings.ToLower(a) == strings.ToLower(b))
fmt.Println()
// TrimSpace: removes leading and trailing spaces and newlines.
padded := " Hello, World! \n"
fmt.Printf("Original: %q\n", padded)
fmt.Printf("After TrimSpace: %q\n", strings.TrimSpace(padded))
fmt.Println()
// Trim: removes specified characters from both ends.
s2 := "***Go Language***"
fmt.Println("Trim('*'):", strings.Trim(s2, "*"))
s3 := "---Hello---"
fmt.Println("TrimLeft('-'):", strings.TrimLeft(s3, "-"))
fmt.Println("TrimRight('-'):", strings.TrimRight(s3, "-"))
fmt.Println()
// TrimPrefix / TrimSuffix: removes a specific string from either end.
url := "https://example.com"
fmt.Println("TrimPrefix:", strings.TrimPrefix(url, "https://"))
filename := "document.pdf"
fmt.Println("TrimSuffix:", strings.TrimSuffix(filename, ".pdf"))
fmt.Println()
// Repeat: repeats a string.
separator := strings.Repeat("=", 20)
fmt.Println(separator)
fmt.Println("Go Dictionary")
fmt.Println(separator)
}
Notes
strings.TrimSpace() is one of the most commonly used functions for sanitizing user input. Use it to strip extra spaces and newlines from form values or configuration file entries. Note that the cutset in strings.Trim() is a set of individual characters, not a substring. To remove a specific substring, use strings.TrimPrefix() or strings.TrimSuffix() instead.
The cutset in strings.Trim(s, cutset) is a set of characters to remove, not a substring. For example, strings.Trim("---hello---", "---") removes the character "-", not the sequence "---". To remove a multi-character sequence, you need a different approach.
For string searching, see strings.Contains(). For conversions between strings and numbers, see strconv.Atoi() / Itoa().
If you find any errors or copyright issues, please contact us.