by @CodingExample

How to trim whitespaces from a string in Swift

Home » swift » How to trim whitespaces from a string in Swift

To remove the leading and trailing white spaces in a string we need to use the trimmingCharacters(in:) method by passing a whitespacescharacter set offered by the swift.

Example:

let str = "  Oxygen    "
let trimmedStr = str.trimmingCharacters(in: .whitespaces)

print(trimmedStr)

Output:

Oxygen

Learn more about trimmingCharacters(in:) method