Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding double quotes to NSString

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!

like image 946
random Avatar asked Mar 27 '13 20:03

random


2 Answers

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"
like image 115
Caleb Avatar answered Nov 10 '22 15:11

Caleb


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

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

Output

"RateThisAppDontAsk" = "Don't ask again"
like image 34
Shamsudheen TK Avatar answered Nov 10 '22 16:11

Shamsudheen TK