String includin Escape character

牧云@^-^@ 提交于 2019-12-13 05:11:02

问题


In textfield when user enter value with escape character, I want to work this same as user enter but it behaves like a string for \e too.

In more detail. suppose user have entered \e[3~ but when i print it in console it shows \e[3~. Actually this should print like ¿[3~ so it will work perfect for me.

I have tried by this line and it works.

NSString * str=[textField.text stringByReplacingOccurrencesOfString:@"\\e" withString:@"\e"];

But suppose if user enter value in caps like \E[3~ then its not replacing. so i have tried with like

NSString * str=[textField.text stringByReplacingOccurrencesOfString:@"\\e" withString:@"\e"];
        str=[textField.text stringByReplacingOccurrencesOfString:@"\\E" withString:@"\E"];

But this is not working in case while user have entered text in small \e[3~. With this when first line will execute to replace \e it will give me perfect result like ¿[3~ but when it will execute next line it gives me same \e[3~ string.

Please suggest me how can i check for both letters and if i can check for all escaping characters at once.


回答1:


The error in your code is that the second line takes the original string textField.text instead of the result from the first line. What you probably meant is:

NSString *str = [textField.text stringByReplacingOccurrencesOfString:@"\\e" withString:@"\e"];
str = [str stringByReplacingOccurrencesOfString:@"\\E" withString:@"\E"];

Alternatively, you can do the replacement in a single step by using the NSCaseInsensitiveSearch option:

NSString *str = [textField.text stringByReplacingOccurrencesOfString:@"\\e"
                        withString:@"\e"
                           options:NSCaseInsensitiveSearch
                             range:NSMakeRange(0, [textField.text length])];


来源:https://stackoverflow.com/questions/19629634/string-includin-escape-character

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!