问题
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