检测 NSString 中的撇号?
我正在使用 Gamekit 通过蓝牙在两个设备之间发送数据。我想获取发送它的设备的名称,但如果名称是“Bob's iPhone”,我想切断“'s iPhone”。我首先检查是否以“iPhone”或“iPod Touch”结尾。
if ([name hasSuffix:@" iPhone"])
{
name = [name substringToIndex:[name length]-7];
}
else if ([name hasSuffix:@" iPod Touch"])
{
name = [name substringToIndex:[name length]-11];
}
但是当我对“'s”做同样的事情时,它永远不会返回 true。此外,撇号看起来与默认撇号略有不同。
if ([name hasSuffix:@"'s"])
{
name = [name substringToIndex:[name length]-2];
}
有什么技巧可以检测撇号吗?我有办法做到这一点吗?
编辑: 左边的撇号是 name 包含的内容,但没有用 hasSuffix:@"'s" 注册。右边的撇号是我添加的撇号。
I'm using Gamekit to send data via bluetooth between two devices. I want to get the name of the device that sent it, but if the name is "Bob's iPhone" I want to cut off the "'s iPhone". I first check for ending in "iPhone" or "iPod Touch".
if ([name hasSuffix:@" iPhone"])
{
name = [name substringToIndex:[name length]-7];
}
else if ([name hasSuffix:@" iPod Touch"])
{
name = [name substringToIndex:[name length]-11];
}
But when I do the same for "'s" it never returns true. Also the apostrophe looks slightly different then the default apostrophe.
if ([name hasSuffix:@"'s"])
{
name = [name substringToIndex:[name length]-2];
}
Is there some trick to detecting apostrophes? Is there a way I can do this?
EDIT:
The apostrophe on the left is what name contains, but is not registering with hasSuffix:@"'s". The apostrophe on the right is the apostrophe I added.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能是编码字符串的情况。您应该确保这种情况下的字符串
是
's
或's
或's
或其他字符。您可能有其他一些性格给您带来问题。
这些是我的猜测。希望这可以帮助您找出问题所在。
It might be the case of encoded string. You should make sure the string about this cases
is it
's
or is it´s
or is it‘s
or some other character.You may have some other character that is causing you problems.
These are my guess. Hoping this helps you to find out your problem.
您想要
rangeOfString:
NSString
中的方法。You want the
rangeOfString:
method inNSString
.