删除 NSString 的最后一个字符

发布于 2024-12-08 06:17:09 字数 88 浏览 2 评论 0原文

我在尝试删除 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

为人所爱 2024-12-15 06:17:10
NSString *string = [NSString stringWithString:@"ABCDEF"];
NSString *newString = [string substringToIndex:[string length]-1];
NSLog(@"%@",newString);

你可以看到 = ABCDE

NSString *string = [NSString stringWithString:@"ABCDEF"];
NSString *newString = [string substringToIndex:[string length]-1];
NSLog(@"%@",newString);

You can see = ABCDE

触ぅ动初心 2024-12-15 06:17:10
NSString = *string = @"abcdef";

string = [string substringToIndex:string.length-(string.length>0)];

是否有字符要删除(即字符串长度大于0)
(string.length>0) 返回 1,从而使代码返回:

 string = [string substringToIndex:string.length-1]; 

如果没有要删除的字符(即字符串的长度不大于 0)
(string.length>0) 返回 0,从而使代码 return:

string = [string substringToIndex:string.length-0]; 

,从而防止崩溃。

NSString = *string = @"abcdef";

string = [string substringToIndex:string.length-(string.length>0)];

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:

 string = [string substringToIndex:string.length-1]; 

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:

string = [string substringToIndex:string.length-0]; 

which prevents crashes.

夏日落 2024-12-15 06:17:10

此代码将仅返回字符串的最后一个字符,而不删除它:

NSString *newString = [oldString substringToIndex:[oldString length]-1];

您可以使用它来删除最后一个字符并保留字符串的剩余值:

str = [str substringWithRange:NSMakeRange(0,[str length] - 1)];

并且还可以使用substringToIndex< /strong> 到 0 长度 的 NSString 将导致崩溃。

您应该在执行此操作之前添加验证,如下所示:

if ([str length] > 0) {

   str = [str substringToIndex:[s length] - 1];

}

这样,使用 substring 方法是安全的。

注意:如果您的应用程序容易崩溃,Apple 将拒绝您的应用程序。

This code will just return the last character of the string and not removing it :

NSString *newString = [oldString substringToIndex:[oldString length]-1];

you may use this instead to remove the last character and retain the remaining values of a string :

str = [str substringWithRange:NSMakeRange(0,[str length] - 1)];

and also using substringToIndex to a NSString with 0 length will result to crashes.

you should add validation before doing so, like this :

if ([str length] > 0) {

   str = [str substringToIndex:[s length] - 1];

}

with this, it is safe to use substring method.

NOTE : Apple will reject your application if it is vulnerable to crashes.

初相遇 2024-12-15 06:17:10

简单且最佳的方法

[mutableString deleteCharactersInRange:NSMakeRange([myRequestString length]-1, 1)];

Simple and Best Approach

[mutableString deleteCharactersInRange:NSMakeRange([myRequestString length]-1, 1)];
一笑百媚生 2024-12-15 06:17:09
NSString *newString = [oldString substringToIndex:[oldString length]-1];

请始终参考文档< /a>:


要包含与您的案例相关的代码:

NSString *str = textField.text;
NSString *truncatedString = [str substringToIndex:[str length]-1];
NSString *newString = [oldString substringToIndex:[oldString length]-1];

Always refer to the documentation:


To include code relevant to your case:

NSString *str = textField.text;
NSString *truncatedString = [str substringToIndex:[str length]-1];
何以笙箫默 2024-12-15 06:17:09

试试这个:

s = [s substringToIndex:[s length] - 1];

Try this:

s = [s substringToIndex:[s length] - 1];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文