ios Objective-C - 从字符串中删除最后一个字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1082178/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:07:14  来源:igfitidea点击:

Objective-C - Remove last character from string

iosobjective-cstringcocoacocoa-touch

提问by Omar

In Objective-C for iOS, how would I remove the last character of a string using a button action?

在 iOS 的 Objective-C 中,如何使用按钮操作删除字符串的最后一个字符?

回答by Marc Charbonneau

In your controller class, create an action method you will hook the button up to in Interface Builder. Inside that method you can trim your string like this:

在您的控制器类中,创建一个操作方法,您将在界面生成器中将按钮连接到该方法。在该方法中,您可以像这样修剪字符串:



if ([string length] > 0) {
    string = [string substringToIndex:[string length] - 1];
} else {
    //no characters to delete... attempting to do so will result in a crash
}











If you want a fancy way of doing this in just one line of code you could write it as:

如果您想在一行代码中以一种奇特的方式执行此操作,您可以将其编写为:

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


*Explanation of fancy one-line code snippet:

If there is a character to delete (i.e. the length of the string is greater than 0)
?????(string.length>0)returns 1thus 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 0thus making the code return:
??????????string = [string substringToIndex:string.length-0];
?????Which prevents crashes.

*花式单行代码片段说明:

如果有要删除的字符(即字符串长度大于0)
????? (string.length>0)返回1从而使代码返回:
?????????? string = [string substringToIndex:string.length-1];

如果没有要删除的字符(即字符串的长度不大于 0)
????? (string.length>0)返回0从而使代码返回:
?????????? string = [string substringToIndex:string.length-0];
?????这可以防止崩溃。

回答by Dave DeLong

If it's an NSMutableString (which I would recommend since you're changing it dynamically), you can use:

如果它是一个 NSMutableString (我会推荐它,因为你正在动态改变它),你可以使用:

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

回答by user1071136

The solutions given here actually do not take into account multi-byte Unicode characters ("composed characters"), and could result in invalid Unicode strings.

这里给出的解决方案实际上没有考虑多字节 Unicode 字符(“组合字符”),并且可能导致无效的 Unicode 字符串。

In fact, the iOS header file which contains the declaration of substringToIndexcontains the following comment:

实际上,包含声明的 iOS 头文件substringToIndex包含以下注释:

Hint: Use with rangeOfComposedCharacterSequencesForRange: to avoid breaking up composed characters

提示:与 rangeOfComposedCharacterSequencesForRange 一起使用:避免分解组合字符

See how to use rangeOfComposedCharacterSequenceAtIndex:to delete the last character correctly.

查看如何rangeOfComposedCharacterSequenceAtIndex:正确删除最后一个字符。

回答by Harald Scheirich

The documentation is your friend, NSStringsupports a call substringWithRangethat can shorten the string that you have an return the shortened String. You cannot modify an instance of NSStringit is immutable. If you have an NSMutableStringis has a method called deleteCharactersInRangethat can modify the string in place

文档是你的朋友,NSString支持substringWithRange可以缩短字符串的调用,你有一个返回缩短的字符串。你不能修改NSString它的一个实例是不可变的。如果你有一个NSMutableString被调用的方法deleteCharactersInRange,可以在适当的地方修改字符串

...
NSRange r;
r.location = 0;
r.size = [mutable length]-1;
NSString* shorted = [stringValue substringWithRange:r];
...