Adding double quotes to NSString

别说谁变了你拦得住时间么 提交于 2019-12-23 09:58:46

问题


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!


回答1:


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"



回答2:


 NSString *output = @"RateThisAppDontAsk";
 NSString *nextString = @"Don't ask again";

 NSString *newOutput = [NSString stringWithFormat:@"\"%@\" = \"%@\"", output, nextString];
 NSLog(@"%@",newOutput);

Output

"RateThisAppDontAsk" = "Don't ask again"



回答3:


This is what you need to do

NSString *firstObject = @"RateThisAppDontAsk";
NSString *secondObject = @"Don't ask again";
NSString *newOutput = [NSString stringWithFormat:@"\"%@\" = \"%@\"",firstObject,secondObject];

Output

test[4556:c07] "RateThisAppDontAsk" = "Don't ask again"


来源:https://stackoverflow.com/questions/15668459/adding-double-quotes-to-nsstring

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