Swift在String上有一个修剪方法吗?例如:
let result = " abc ".trim()
// result == "abc"
以下是从 String
的开头和结尾删除所有空格的方法。
(使用Swift 2.0测试的示例.)
let myString = " Let's trim all the whitespace
"
let trimmedString = myString.stringByTrimmingCharactersInSet(
NSCharacterSet.whitespaceAndNewlineCharacterSet()
)
// Returns "Let's trim all the whitespace"
(使用Swift 3测试的示例.)
let myString = " Let's trim all the whitespace
"
let trimmedString = myString.trimmingCharacters(in: .whitespacesAndNewlines)
// Returns "Let's trim all the whitespace"
希望这有所帮助.