我试图用另一个字符替换字符串中的所有撇号(')字符。我正在运行以下代码:
someString.replacingOccurrences(of: apost, with: "a")
我曾试图让阿普斯特平等对待以下各点:
apost = "\'"
apost = #"'"#
apost = "'"
他们都没有从someString中删除撇号。
如果有办法让Swift代替撇号,请告诉我。谢谢。
发布于 2020-03-28 03:09:05
replacingOccurrences(...)
方法返回一个新的字符串对象,并替换撇号。原始的someString
对象不会因此而改变。你需要:
someString = someString.replacingOccurences(...)
如果发生了这种情况,在Xcode中打开更多警告(或者查看警告)。在我的设置中,由于未使用的返回值,这不会进行编译。
发布于 2021-07-18 09:59:39
我有同样的问题,它不承认撇号或单引号。若要查找和替换它们,请对每个字符使用unicode。这对我有用。
let str = "mark's new name is 'mike'"
// apostrophe
var modstr = str.replacingOccurences(of: "\u{0027"}, with "")
// left single quote
modstr = modstr.replacingOccurences(of: "\u{2018"}, with "")
// right single quote
modstr = modstr.replacingOccurences(of: "\u{2019"}, with "")
// output
print(modstr)
输出:
marks new name is mike
发布于 2021-09-15 14:19:35
我也有过同样的问题。这是因为撇号不是正确的字符;它应该是一个排版(或“卷”)撇号(’
),而不是打字机(或“直”)撇号('
)。复制并粘贴下面的代码,或者只是正确的撇号字符:
searchQuery = searchQuery.replacingOccurrences(of: "’", with: "")
https://stackoverflow.com/questions/60900301
复制相似问题