我正在使用“ trimmingCharacters”从字符串中删除空格,但是空格并未删除。
let number = "123 456"
let replaced = number.trimmingCharacters(in: .whitespaces)
print(username)
print(replaced)
答案 0 :(得分:1)
在replacingOccurrences(of:with:)
上使用 String
方法,即
let number = "123 456"
let replaced = number.replacingOccurrences(of: " ", with: "")
print(replaced) //Output: 123456
答案 1 :(得分:0)
使用字符串 replacingOccurrences
let number = "123 456"
let replaced = number.replacingOccurrences(of: " ", with: "")
print(replaced)
或
使用过滤器
let removed = number.filter{!$0.isWhitespace}
print(removed)
输出123456