Home
Go
strings.ToLower, ToUpper Examples
This page was last reviewed on Dec 16, 2021.
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.
Result The before and after values are printed with fmt.Println. The string was lowercased.
fmt
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.
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.
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).
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 16, 2021 (edit).
Home
Changes
© 2007-2024 Sam Allen.