[英]How to force Swift to recognize apostrophe?
I am trying to replace all apostrophe (') characters in a string with another character.我正在尝试用另一个字符替换字符串中的所有撇号 (') 字符。 I am running this code:
我正在运行此代码:
someString.replacingOccurrences(of: apost, with: "a")
I have tried to let apost equal the following:我试图让post等于以下内容:
apost = "\'"
apost = #"'"#
apost = "'"
None of them have removed the apostrophe from someString.他们都没有从 someString 中删除撇号。
Please let me know if there is a way to get Swift to replace apostrophe's.如果有办法让 Swift 替换撇号,请告诉我。 Thank you.
谢谢你。
someString.replacingOccurrences(...) returns a new string object with apostrophes replaced. someString.replacingOccurrences(...) 返回一个替换了撇号的新字符串对象。 The original someString isn't changed by this.
原来的 someString 不会因此而改变。 You need
你需要
someString = someString.replacingOccurences(...). someString = someString.replacingOccurences(...)。
If that's what's happened turn more warnings on in Xcode (or look at warnings).如果发生这种情况,请在 Xcode 中打开更多警告(或查看警告)。 On my setup, this wouldn't have compiled because of the unused return value.
在我的设置中,由于未使用的返回值,这不会被编译。
I am trying to replace all apostrophe (') characters in a string with another character.我试图用另一个字符替换字符串中的所有撇号 (') 字符。 I am running this code:
我正在运行此代码:
someString.replacingOccurrences(of: apost, with: "a")
I have tried to let apost equal the following:我试图让apost等于以下内容:
apost = "\'"
apost = #"'"#
apost = "'"
None of them have removed the apostrophe from someString.他们都没有从 someString 中删除撇号。
Please let me know if there is a way to get Swift to replace apostrophe's.请让我知道是否有办法让 Swift 替换撇号。 Thank you.
谢谢你。
I had the same issue with not recognizing apostrophies or single quotes.我遇到了同样的问题,无法识别撇号或单引号。 To find and replace them, use the unicode for each character.
要查找和替换它们,请对每个字符使用 unicode。 This worked for me.
这对我有用。
let str = "mark's new name is 'mike'"
var modstr = str.replacingOccurences(of: "\u{0027"}, with "") //
apostrophe
modstr = modstr.replacingOccurences(of: "\u{2018"}, with "")
// left single quote
modstr = modstr.replacingOccurences(of: "\u{2019"}, with "")
// right single quote
print(modstr)
// modstr = marks new name is mike
I had the same issue.我遇到过同样的问题。 It's because the apostrophe is not the right character;
这是因为撇号不是正确的字符; it should be a typographic (or “curly”) apostrophe (
'
), not a typewriter (or “straight”) apostrophe ( '
).它应该是印刷(或“卷曲”)撇号(
'
),而不是打字机(或“直”)撇号 ( '
)。 Copy and paste the code below, or just the correct apostrophe character:复制并粘贴以下代码,或仅粘贴正确的撇号字符:
searchQuery = searchQuery.replacingOccurrences(of: "’", with: "")
You can assign a comparison character for apostrophe as char charToCompare = (char)8217;您可以将撇号的比较字符指定为 char charToCompare = (char)8217;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.