How to trim whitespaces from a string in Swift
In this tutorial, we are going to learn about how to trim the whitespaces from a string in Swift.
To remove the leading and trailing whitespaces from a string, we can use
the trimmingCharacters(in:)
method by passing a whitespaces
character set
offered by the swift.
Here is an example:
let str = " Oxygen "
let trimmedStr = str.trimmingCharacters(in: .whitespaces)
print(trimmedStr)
Output:
Oxygen