In Swift we can trim from string all symbols that we want. If we want to remove white spaces and new lines, we need to use following:
let str = " Hello World "
let trimmed = str.trimmingCharacters(in: .whitespacesAndNewlines)
This string will be trimmed to "Hello World"
For more handy using we can write extension for String class
import Foundation
import UIKit
extension String {
func trim() -> String {
return self.trimmingCharacters(in: CharacterSet.whitespaces)
}
}
And here we use this extension
let result = " Hello World ".trim()
print(result)
// "Hello World"
No comments:
Post a Comment