Swift – Convert a String to Uppercase
To convert a String to Uppercase in Swift, use String.uppercased()
function. Call uppercased() function on the String. The function returns a new string with the contents of the original string transformed to uppercase alphabets.
The syntax to convert a String str
to Uppercase is
str.uppercased()
uppercased()
function returns a new String.
Example
In this example, we will take a string in variable str
and convert this string to uppercase using String.uppercased() function.
main.swift
import Foundation var str = "Hello World" var result = str.uppercased() print("Original String : \(str)") print("Uppercase String : \(result)")
Output
Original String : Hello World Uppercase String : HELLO WORLD
ADVERTISEMENT
Conclusion
In this Swift Tutorial, we learned how to convert a string to uppercase using String.uppercased() function.