Changing the case of a string
Swift strings have two built-in methods for case: uppercased() and lowercased(). They need no import. Each one converts every letter and leaves other characters, such as digits, spaces and punctuation, as they are.
let word = "Hello World"
let upper = word.uppercased()
let lower = word.lowercased()
print(upper)
print(lower)
print(word)
HELLO WORLD
hello world
Hello World
Neither method changes word. Both return a new string, as the last line shows. The -ed ending is Swift's naming convention: methods named like this (uppercased, sorted, reversed) return a new value and leave the original alone.
Comparing without caring about case
Strings are compared character by character, so "Hello" and "hello" are different. A common trick is to convert both sides to the same case first:
let a = "Hello"
let b = "hello"
print(a == b)
print(a.lowercased() == b.lowercased())
false
true
This works for user input such as a typed answer or a command, where capital letters shouldn't change the meaning.
Capitalizing the first letter
There is no built-in method that capitalizes only the first letter. Combine prefix, dropFirst and uppercased():
let text = "hello world"
let capitalized = text.prefix(1).uppercased() + text.dropFirst()
print(capitalized)
Hello world
prefix(1) is the first character as a Substring, and uppercased() turns it into a String. Adding the rest of the text with + produces a new string.
Checking the case
To ask about the case of existing text, use the isUppercase and isLowercase properties of a Character. A string is already all uppercase when it equals its own uppercased version:
let word = "Hello World"
print(word.first!.isUppercase, word.last!.isUppercase)
print(word.filter { $0.isUppercase }.count)
print(word == word.uppercased())
true false
2
false
The second line counts the capital letters by keeping only the uppercase characters and measuring what is left.
Details worth knowing
- The methods follow Unicode rules, so accented letters convert too:
"ÉCOLE".lowercased()givesécole. - The result can be longer than the original.
"straße".uppercased()isSTRASSE, because the uppercase form ofßis two letters. - A single
Characterhas the same methods, butuppercased()on aCharacterreturns aString, since the result may be more than one character.
Chaining with other methods
Since each call returns a string, case changes combine with other string methods. For example, " Hello ".lowercased().count is 9: lowercasing doesn't change the length of this text, and the spaces are still counted. Convert the case first or last depending on what the next step needs, but remember that each call creates a fresh string and leaves the one before it unchanged.
The exercise
The constant word holds "Hello World". You add two constants, one per step. Declare upper with let and assign it word.uppercased(). Then declare lower in the same way with word.lowercased(). The two print calls in the starter code then show them.
let word = "Hello World"
let upper = word.uppercased()
let lower = word.lowercased()
print(upper)
print(lower)
HELLO WORLD
hello world
Common mistakes
- Calling the method and discarding the result. Writing
word.uppercased()on its own line changes nothing; the new string must be stored or printed. - Forgetting the parentheses.
uppercasedwithout()is the method itself, not its result. - Using the wrong spelling. The methods are
uppercased()andlowercased(), with the-ed. - Using
letfor something you want to reassign. To replace the original variable, declare it withvarand writeword = word.uppercased().
Key takeaways
uppercased()andlowercased()return a new string and leave the original unchanged.- Convert both sides to one case to compare strings without regard to case.
- Only letters change. Digits, spaces and punctuation stay the same.
