Convert all characters in the Swift string to uppercase

This tutorial contains information on converting all characters in the string to uppercase in Swift. We will declare a string that will not necessarily be in uppercase but the output results in uppercase letters.

uppercased() method

The uppercased() method in Swift to convert all the letters into uppercase from any case in a given string. When we implement it, the method converts all the letters in a string into uppercase and also does not take any parameter.

Let’s now move forward to the implementation where the first part will convert the string into uppercase and the second part will compare the two strings after converting into uppercase.

Converting a string into uppercase in Swift

We are now familiar with the uppercased() method. Now we will create a string and then convert it into uppercase using the above-mentioned method.

Snippet part-

var str1="coding is fun"
print(str1.uppercased())

Output-

CODING IS FUN

Compare two strings implementing uppercased()

We now know how to convert lowercase into uppercase and it also works fine when both the cases are present, but if we want to compare two strings where case does not matter, like as “Code” and “code” results in false because of cases but we need to make it true, we can convert the case into uppercase and check for the condition as-

var str1="CODING IS FUN"
var str2="CODING IS fun"
print(str1.uppercased()==str2.uppercased())

Output-

true

Here in this particular article, we have worked on uppercased() method and implemented the same where we first tried to convert the case from lower to upper and compared two strings after implementing uppercased().

We have just learned how to convert the string into uppercase.

Leave a Reply

Your email address will not be published. Required fields are marked *