字符串包含转义字符 [英] String includin Escape character

查看:158
本文介绍了字符串包含转义字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在用户输入转义字符的值的文本框中,我想要像用户输入一样工作,但它的行为类似于\e的字符串。更多详情。
假设用户输入了\e [3〜但是当我在控制台中打印它显示\e [3〜。实际上这应该像¿[3〜,所以它将适合我的工作。



我已经尝试了这一行,它的工作。

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

但假设用户输入的值如\E [3〜然后不更换。所以我已经尝试过像

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

但是,当用户输入小\e [3〜。有了这个,当第一行执行替换\e它会给我完美的结果像¿[3〜但当它将执行下一行它给我相同的\e [3〜字符串。



请建议我如何检查这两个字母,如果我可以一次检查所有转义的字符。

解决方案

您的代码中的错误是第二行使用原始字符串 textField.text
而不是第一行。你可能意味着:

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

或者,您可以通过使用
NSCaseInsensitiveSearch 选项:

  NSString * str = [textField.text stringByReplacingOccurrencesOfString:@\\ \\\e
withString:@\e
选项:NSCaseInsensitiveSearch
范围:NSMakeRange(0,[textField.text length])];


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.

解决方案

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])];

这篇关于字符串包含转义字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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