Put this code on a file on your project, something likes Utils.swift:
extension String
{
func trim() -> String
{
return self.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
}
}
So you will be able to do this:
let result = " abc ".trim()
// result == "abc"
Swift 3.0 Solution
extension String
{
func trim() -> String
{
return self.trimmingCharacters(in: NSCharacterSet.whitespaces)
}
}
So you will be able to do this:
let result = " Hello World ".trim()
// result = "HelloWorld"