空格未从字符串中删除

时间:2019-06-12 11:10:22

标签: ios swift

我正在使用“ trimmingCharacters”从字符串中删除空格,但是空格并未删除。


    let number = "123 456"
    let replaced = number.trimmingCharacters(in: .whitespaces)
     print(username)
     print(replaced)

2 个答案:

答案 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