删除前导和尾随空格:
let trimmedString = string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
Swift 3.0:
let trimmedString = string.trimmingCharacters(in: .whitespaces)