Home
Search
Golang ToLower, ToUpper String ExamplesUse the strings.ToLower and ToUpper funcs to lowercase and uppercase strings.
dot net perls
ToLower, ToUpper. There are lowercase and uppercase letters. To change from one to the other, we can use functions from the strings package in Go.
Simple methods. The strings.ToLower and ToUpper funcs have the expected results. They do not change characters like digits, spaces, or punctuation. Title() capitalizes all words.
ToLower example. Let us begin with strings.ToLower. We have a string literal with the value of "Name." When we call ToLower, a new string is returned.
And The uppercase character in the string was changed to a lowercase one—but only in the copied, returned string.
Tip The original string (value) is left alone after strings.ToLower returns. This is a behavior shared by many modern languages.
Golang program that uses ToLower
package main import ( "fmt" "strings" ) func main() { value := "Name" // Convert to lowercase. valueLower := strings.ToLower(value) fmt.Println("BEFORE:", value) fmt.Println("AFTER: ", valueLower) }
BEFORE: Name AFTER: name
ToUpper example. The ToUpper func works in the same way as ToLower. It returns a new, copied version of the string argument that has lowercase chars changed to uppercase ones.
Golang program that uses ToUpper
package main import ( "fmt" "strings" ) func main() { value := "Dot Net Perls" // Use ToUpper. valueUpper := strings.ToUpper(value) fmt.Println("BEFORE:", value) fmt.Println("AFTER: ", valueUpper) }
BEFORE: Dot Net Perls AFTER: DOT NET PERLS
Title. The strings package provides ways to change the case of characters. Here we apply title-casing to a string with the Title func. The result is a properly-capitalized string.
Golang program that uses Title func
package main import ( "fmt" "strings" ) func main() { name := "the apology" // Capitalize this string. result := strings.Title(name) fmt.Println(result) }
The Apology
A summary. For real-world programs in Go, we often need methods like strings.ToLower and ToUpper. These can help us simplify our programs (as by using only lowercase-based logic).
Home
© 2007-2021 sam allen. see site info on the changelog