如何从NSString中删除最后一个unicode符号 [英] How to remove the last unicode symbol from NSString

查看:237
本文介绍了如何从NSString中删除最后一个unicode符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了与文本字段关联的自定义键盘,因此当用户按下删除按钮时,我会从字符串中删除最后一个字符,并手动更新当前文本字段文本。

I have implemented a custom keyboard associated with a text field, so when the user presses the delete button, I remove the last character from the string, and manually update the current text field text.

NSRange range = NSMakeRange(currentTextFieldString.length-1, 1);
[currentTextFieldString replaceCharactersInRange:range withString:@""];

到目前为止一直很好。

现在,问题是,用户可以选择输入一些特殊的unicode符号,这些不是1个字节,它们也可以是2个字节,现在按下删除按钮,我必须删除整个符号,但如果我按照上述方法,用户必须按两次删除按钮。

Now, the problem is, that the user has the option to enter some special unicode symbols, these are not 1 byte, they can be 2 bytes too, now on pressing the delete button, I have to remove the entire symbol, but if I follow the above approach, the user has to press the delete button twice.

在这里,如果我这样做:

Here, if I do:

NSRange range = NSMakeRange(currentTextFieldString.length-2, 2);
[currentTextFieldString replaceCharactersInRange:range withString:@""];

它工作正常,但是,正常字符只有1个字节,会被删除两次一段时间。

it works fine, but then, the normal characters, which are just 1 byte, get deleted twice at a time.

如何处理这种情况?

提前致谢。

编辑:

奇怪的是,如果我切换到iPhone键盘,它会适当地处理这两种情况。必须有一些方法可以做到这一点,有一些我缺少的东西,但我无法弄清楚是什么。

It is strange, that if I switch to the iPhone keyboard, it handles both cases appropriately. There must be some way to do it, there is something that I am missing, but am not able to figure out what.

推荐答案

这是问题所在。 NSStrings使用UTF-16编码。许多常见的Unicode字形只占用一个 unichar (16位无符号值)。然而,一些字形占据了两个unichars。更糟糕的是,一些字形可以被组合或分解,例如,可能是一个Unicode代码点,或者它可能是两个 - 一个急性重音后跟一个e。这使你很难做到你想要的东西即删除一个角色,因为很难说它占用了多少个unichars。

Here's the problem. NSStrings are encoded using UTF-16. Many common Unicode glyphs take up only one unichar (a 16 bit unsigned value). However, some glyphs take up two unichars. Even worse, some glyphs can be composed or decomposed, e.g.é might be one Unicode code point or it might be two - an acute accent followed by an e. This makes it quite difficult to do what you want viz delete one "character" because it is really hard to tell how many unichars it takes up.

幸运的是,NSString有一个方法可以帮助解决这个问题:-rangeOfComposedCharacterSequenceAtIndex:。你需要做的是得到最后一个unichar的索引,在它上面运行这个方法,返回的NSRange将告诉你从哪里删除。它是这样的(未经测试):

Fortunately, NSString has a method that helps with this: -rangeOfComposedCharacterSequenceAtIndex:. What you need to do is get the index of the last unichar, run this method on it, and the returned NSRange will tell you where to delete from. It goes something like this (not tested):

NSUInteger lastCharIndex = [myString length] - 1; // I assume string is not empty
NSRange rangeOfLastChar = [myString rangeOfComposedCharacterSequenceAtIndex: lastCharIndex];
myNewString = [myString substringToIndex: rangeOfLastChar.location];

这篇关于如何从NSString中删除最后一个unicode符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆