如何在 Swift 中删除字符串中的空格? [英] How to remove whitespaces in strings in Swift?
本文介绍了如何在 Swift 中删除字符串中的空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这有效
let replaced = String(map(aString.generate()) {
$0 == " " ? "-" : $0 })
这没有
let replaced = String(map(aString.generate()) {
$0 == " " ? "" : $0 })
为什么?
推荐答案
对于 Swift 5:
" spaces here ".replacingOccurrences(of: " ", with: "")
返回:
"spaceshere"
这篇关于如何在 Swift 中删除字符串中的空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文