string xcode 从字符串中删除最后一个字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5718570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
xcode cut last characters from string
提问by DennisW83
I am working on a Xcode
program and need to cut the last 5 characters from a string.
我正在开发一个Xcode
程序,需要从字符串中删除最后 5 个字符。
Does anybody know how to cut the last 5 characters from a NSString
?
有人知道如何从 a 中删除最后 5 个字符NSString
吗?
回答by Philip Kramer
just looked for it my self few minutes ago
几分钟前我自己找的
NSString *str = @"1234567890";
NSString *newStr;
newStr = [str substringToIndex:[str length]-5];
NSLog(@"%@", newStr);
回答by Heider Sati
I hope it's not too late for this answer:
我希望这个答案还为时不晚:
NSString *str = @"1234567890";
NSString *newStr;
newStr = [str substringWithRange:NSMakeRange(str.length -1, 1)];
NSLog(@"%@", newStr);
Although you provided a string of "1234567889" and the above code should work fine, however, in real life if it's a different string then please check to ensure that it's not empty before doing the above, otherwise, str.length would return -1 causing an error.
尽管您提供了一个字符串“1234567889”并且上面的代码应该可以正常工作,但是,在现实生活中,如果它是一个不同的字符串,那么请在执行上述操作之前检查以确保它不是空的,否则 str.length 将返回 -1导致错误。
I hope this helps.
我希望这有帮助。
Kind Regards, Heider Sati
亲切的问候,海德·萨蒂