The uppercased()
method converts all lowercase characters in a string into uppercase characters.
Example
var message = "swift is fun"
// convert message to uppercase
print(message.uppercased())
// Output: SWIFT IS FUN
uppercased() Syntax
The syntax of uppercased()
method is:
string.uppercased()
Here, string is an object of the String
class.
uppercased() Parameters
uppercased()
method doesn't take any parameters.
uppercased() Return Value
returns the given string in uppercase.
Example 1: Swift uppercased()
var string = "this should be uppercase!"
print(string.uppercased())
// string with numbers
// all alphabets should be uppercase
var string2 = "Th!s Sh0uLd B3 uPp3rCas3!"
print(string2.uppercased())
Output
THIS SHOULD BE UPPERCASE! TH!S SH0ULD B3 UPP3RCAS3!
Example 2: Compare Two Strings Using uppercased()
// first string
var str1 = "swift is awesome!"
// second string
var str2 = "SwIfT Is AwEsOmE!"
// compare two strings
if(str1.uppercased() == str2.uppercased()) {
print("The strings are same.")
}
else {
print("The strings are not same.")
}
Output
The strings are same.