In this Swift programming tutorial, you are going to learn how to convert a string to lowercase or uppercase.
In Swift, there are already in-built methods available that can help us to transform any string to lowercase or uppercase. One is the uppercased()
and the other one is the lowercased()
.
Below is the syntax of the uppercased method:
string.uppercased()
And below is the syntax for the lowercased() method:
string.lowercased()
Let’s see an example of transforming a string from uppercase to lowercase using the Swift lowercased()
method:
let color = "YELLOW"
let transformed = color.lowercased()
print(transformed)
Output:
yellow
In the above program, we have a string color
. We applied the lowercased()
method to the string and store the new string in a constant transformed
.
Now below is an example of converting a string from lowercase to uppercase using the uppercased()
method:
let color = "green"
let transformed = color.uppercased()
print(transformed)
Output:
GREEN