在方法调用stringByReplacingOccurrencesOfString中转义反斜杠在目标C中不起作用 [英] Escaping backslashes in method call stringByReplacingOccurrencesOfString Does not work in Objective C

查看:50
本文介绍了在方法调用stringByReplacingOccurrencesOfString中转义反斜杠在目标C中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这行代码不会产生我期望的结果.

This line of code does not produce the result I am expecting.

NSString *tmpstoryTitle2 = [tmpstoryTitle stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];

我正在尝试更改这样的字符串:

I am attempting to change a string like this:

你好,"this"是我的字符串.

hello, "this" is my string.

你好,\"this \"是我的字符串.

hello, \"this\" is my string.

使用上面的代码.但是,我得到的输出是:

With the code above. However, the output I get is:

你好,\\"this \\"是我的字符串.

hello, \\"this\\" is my string.

如果我从搜索字符串\\中删除\\以替换\",则会得到输出:

If I remove the \\ from the search string \\" to replace \" I get the output:

你好,"this"是我的字符串.

hello, "this" is my string.

这是正确的,但是我无法在我的前面加上转义序列的\开头,对于反斜杠使用\\,对于双引号则使用\".

Which is right, but I am unable to add the \ infront of my " with the escape sequence, \\ for backslash and \" for double quotes.

推荐答案

您如何验证字符串?如果将其打印到控制台(使用NSLog()),则会为您插入那些前导的反斜杠;尝试为其设置UILabel的文本,然后查看它是否以这种方式显示.您的代码看起来正确.

How are you verifying the string? If you print it to the console (using NSLog()), those leading backslashes will be inserted for you; try setting a UILabel's text to it, and see if it shows up that way. Your code looks correct.

这篇关于在方法调用stringByReplacingOccurrencesOfString中转义反斜杠在目标C中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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