在NSString中添加双引号

时间:2013-03-27 20:10:32

标签: objective-c nsstring formatting

我已阅读所有内容并且“无法正常工作。

我正在尝试创建一个如下所示的字符串:

“SomeString”=“AnotherString”

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

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

它输出的全部是:

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

我想也许“=”会导致问题,但删除仍然会给我一个输出:

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

非常感谢任何帮助!

3 个答案:

答案 0 :(得分:25)

在一个小的MacOS X命令行测试程序中为我工作。这是所有代码:

#import <Foundation/Foundation.h>

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

输出是:

test[54844:403] foo " = " bar

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

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

新输出是:

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

答案 1 :(得分:3)

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

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

<强>输出

"RateThisAppDontAsk" = "Don't ask again"

答案 2 :(得分:0)

这是你需要做的事情

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

输出

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