swift在String上trim方法
swift在String上trim方法
这是从的开头和结尾删除所有空白的方法String。
(示例使用 Swift 2.0 进行了测试。)
let myString = " \t\t Let's trim all the whitespace \n \t \n "
let trimmedString = myString.stringByTrimmingCharactersInSet(
NSCharacterSet.whitespaceAndNewlineCharacterSet()
)
// Returns "Let's trim all the whitespace"
(示例使用 Swift 3+ 进行了测试。)
let myString = " \t\t Let's trim all the whitespace \n \t \n "
let trimmedString = myString.trimmingCharacters(in: .whitespacesAndNewlines)
// Returns "Let's trim all the whitespace"