将双引号添加到NSString [英] Adding double quotes to NSString

查看:548
本文介绍了将双引号添加到NSString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试图创建一个如下所示的字符串:

SomeString=AnotherString

以下是我必须插入双引号的代码:

  NSString * newOutput = [NSString stringWithFormat:@%@ \= \%@,output,[englishDic objectForKey:str]]; 

所有它输出的是:

<$ p $
RateThisAppDontAsk \= \不要再询问



<

 RateThisAppDontAsk \ \不要再询问

任何帮助都将非常感谢!

解决方案


$ b在一个小小的MacOS X命令行测试程序中工作。 $ b

  #import&l t; Foundation / Foundation.h> 

int main(int argc,const char * argv [])
{
@autoreleasepool {
NSString * newOutput = NSString stringWithFormat:@%@ \= \%@,@foo,@bar];
NSLog(newOutput);
}
return 0;



$ b $输出是:

$ b pre> test [54844:403] foo=bar

如果您要在 foo 之前和 bar 之后加引号,请添加以下内容:

  NSString * newOutput = [NSString stringWithFormat:@\%@ \= \%@ \,@foo,@bar]; 

新的输出是:

  test [54873:403]foo=bar


I have reading everything and \" does NOT work.

I am trying to create a string that looks like this:

"SomeString" = "AnotherString"

Here is the code that I have to insert the double quotes:

NSString *newOutput = [NSString stringWithFormat:@"%@ \" = \" %@", output, [englishDic objectForKey:str]];

All that it outputs is:

"RateThisAppDontAsk \" = \" Don't ask again"

I thought maybe the "=" was causing problems but removing still gives me an output of this:

"RateThisAppDontAsk \"  \" Don't ask again"

Any help would be very much appreciated!

解决方案

Works for me in a little MacOS X command line test program. Here's all the code:

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        NSString *newOutput = [NSString stringWithFormat:@"%@ \" = \" %@", @"foo", @"bar"];
        NSLog(newOutput);
    }
    return 0;
}

Output is:

test[54844:403] foo " = " bar

If you want quotes before foo and after bar, add those:

NSString *newOutput = [NSString stringWithFormat:@"\"%@\" = \"%@\"", @"foo", @"bar"];

New output is:

test[54873:403] "foo" = "bar"

这篇关于将双引号添加到NSString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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