To convert a string to uppercase in Swift, you can use the uppercased()
method of the String class.
In this example,
str1
with a string value of "Hello World"
.uppercased()
method of the string object str1
to convert it to uppercase and store the result in the variable upperCaseStr1
.uppercased()
method returns a new string with all characters converted to uppercase.upperCaseStr1
.let str1 = "Hello World"
let upperCaseStr1 = str1.uppercased()
print("Uppercase of given string is: \(upperCaseStr1)")
Uppercase of given string is: HELLO WORLD
In this example,
str2
with a string value of "apple"
.uppercased()
method of the string object str2
to convert it to uppercase and store the result in the variable upperCaseStr2
.uppercased()
method returns a new string with all characters converted to uppercase.upperCaseStr2
.let str2 = "apple"
let upperCaseStr2 = str2.uppercased()
print("Uppercase of given string is: \(upperCaseStr2)")
Uppercase of given string is: APPLE
In this tutorial, we learned How to convert String to Uppercase in Swift language with well detailed examples.