Changing the case of a string
Go converts case with two functions in the strings package. strings.ToUpper(s) returns s with every letter in uppercase, and strings.ToLower(s) returns it in lowercase. Characters that are not letters, such as spaces, digits and punctuation, stay the same.
s := "Hello World"
upper := strings.ToUpper(s)
lower := strings.ToLower(s)
fmt.Println(upper) // HELLO WORLD
fmt.Println(lower) // hello world
These are functions that take the string as an argument, not methods. Writing s.ToUpper() does not work in Go.
Strings are immutable
The functions never change the string you pass in. Go strings can't be modified, so each call builds and returns a new string. After the code above, s is still "Hello World". Keep the result in a variable, as the lesson does, or assign it back with s = strings.ToLower(s) when you don't need the original.
Exercise step by step
- Start from the given variable.
wordholds"Hello World". - Create
upperwith:=. Assign itstrings.ToUpper(word). - Create
lowerwith:=. Assign itstrings.ToLower(word), on the line below.
The solution in Go
word := "Hello World"
upper := strings.ToUpper(word)
lower := strings.ToLower(word)
Afterwards, upper is "HELLO WORLD", lower is "hello world", and word is untouched.
Comparing without regard to case
A common reason to change case is to compare strings that may differ only in capitals. Converting both sides to the same case works, but Go also has a function made for it:
fmt.Println("Go" == "GO") // false
fmt.Println(strings.EqualFold("Go", "GO")) // true
The == operator is case-sensitive, so "Go" and "GO" are different strings. strings.EqualFold compares them ignoring case, without building two new strings.
Non-English text
Both functions understand Unicode, not just English letters, so strings.ToUpper("héllo") gives "HÉLLO". Case rules in some languages are not simple one-to-one mappings, so for text beyond ASCII, use EqualFold rather than a hand-made comparison when you can.
Common mistakes
- Ignoring the return value. A call such as
strings.ToUpper(word)on its own line compiles, but changes nothing you can see, because the new string is thrown away. - Calling it as a method.
word.ToUpper()andword.upper()don't exist in Go. - Forgetting the import. The file must import
"strings". - Mixing up the names. The functions are
ToUpperandToLower, with capitals onTand onUorL. Go treatsToupperas a different, undefined name. - Changing only the first letter.
ToUpperconverts every letter. To capitalise only the first letter, combine it with slicing:strings.ToUpper(s[:1]) + s[1:], which works for non-empty ASCII text.
Where changing case is useful
Changing case is usually a preparation step. Before you search a sentence for a word with strings.Index or strings.Contains, convert both the sentence and the word to lowercase so that "Hello" and "hello" count as the same. Before you count letters or look words up in a collection, convert to one case so that capitalised and plain forms are treated alike. The conversion costs a new string each time, which is fine for the short strings in this chapter.
The conversion also runs both ways. Calling strings.ToLower on text that is already lowercase returns the same text, and calling strings.ToUpper on digits or symbols leaves them alone, so you don't need to check the content first.
A short example with both
Suppose you want to check whether a message contains the word "go" however it is capitalised. Lower the message first, then search:
msg := "Learning GO is fun"
fmt.Println(strings.Contains(strings.ToLower(msg), "go")) // true
The inner call returns "learning go is fun", and the outer call searches that. The original msg keeps its capitals, so you can still print it as the user typed it. This pattern, convert a copy for the comparison and keep the original for display, comes up constantly in real programs.
Key takeaways
strings.ToUpper(s)andstrings.ToLower(s)return a new string with every letter converted.- The original string is not changed, so store the result.
- For case-insensitive comparison, use
strings.EqualFold.