删除 NSString 的最后一个字符
我在尝试删除 NSString 的最后一个字符时遇到了一些麻烦。 我是 Objective-C 的新手,我不知道如何做到这一点。
你们能点亮我吗?
I've got some trouble 'ere trying to remove the last character of an NSString.
I'm kinda newbie in Objective-C and I have no idea how to make this work.
Could you guys light me up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你可以看到 = ABCDE
You can see = ABCDE
是否有字符要删除(即字符串长度大于0)
(string.length>0)
返回 1,从而使代码返回:如果没有要删除的字符(即字符串的长度不大于 0)
(string.length>0)
返回 0,从而使代码 return:,从而防止崩溃。
If there is a character to delete (i.e. the length of the string is greater than 0)
(string.length>0)
returns 1, thus making the code return:If there is NOT a character to delete (i.e. the length of the string is NOT greater than 0)
(string.length>0)
returns 0, thus making the code return:which prevents crashes.
此代码将仅返回字符串的最后一个字符,而不删除它:
您可以使用它来删除最后一个字符并保留字符串的剩余值:
并且还可以使用substringToIndex< /strong> 到 0 长度 的 NSString 将导致崩溃。
您应该在执行此操作之前添加验证,如下所示:
这样,使用 substring 方法是安全的。
注意:如果您的应用程序容易崩溃,Apple 将拒绝您的应用程序。
This code will just return the last character of the string and not removing it :
you may use this instead to remove the last character and retain the remaining values of a string :
and also using substringToIndex to a NSString with 0 length will result to crashes.
you should add validation before doing so, like this :
with this, it is safe to use substring method.
NOTE : Apple will reject your application if it is vulnerable to crashes.
简单且最佳的方法
Simple and Best Approach
请始终参考文档< /a>:
substringToIndex:
length
要包含与您的案例相关的代码:
Always refer to the documentation:
substringToIndex:
length
To include code relevant to your case:
试试这个:
Try this: