代码之家  ›  专栏  ›  技术社区  ›  Jonah

是否可以在nsstring中包含引号?

  •  39
  • Jonah  · 技术社区  · 15 年前

    谢谢

    6 回复  |  直到 15 年前
        1
  •  110
  •   Jeff Kelley    15 年前

    当然,你只需要避开引号。

    NSString *someString = @"This is a quotation mark: \"";
    NSLog(@"%@", someString );
    

    This is a quotation mark: "
    
        2
  •  17
  •   Bhavin    11 年前

    你可以用 双引号转义序列 :

    NSString *str = @"Hello \"World\"";
    NSLog(@"Output : %@",str);
    
    Output : Hello "World"
    

    逃逸序列

    \b    Backspace
    \f    Form Feed
    \n    Newline
    \t    Horizontal Tab
    \v    Vertical Tab
    \\    Backslash
    \’    Single Quote
    \”    Double Quote
    \?    Question Mark
    
        3
  •  6
  •   TheTiger    6 年前

    反斜杠的用法 \" 已经提到了,所以我的回答不同。你可以用 ASCII Code

    字符码 .

     NSString *str = [NSString stringWithFormat:@"%cThis is a quotation mark: %c", 34, 34];
     NSLog(@"%@", str);
    

    输出为:

    Swift 4.0版

    let str = String(format: "%cThis is a quotation mark: %c", 34, 34)
    print(str)
    
        4
  •  4
  •   Jiří Zahálka    10 年前

    敏捷的

    let string = " TEST  \"  TEST "
    println(string)
    

    控制台中的输出为-TEST“TEST

        5
  •  3
  •   John Calsbeek    15 年前

    是的,您可以在列表中包含引号 NSString 使用反斜杠转义的文本。

    Quote " Quote

    @"Quote \" Quote"
    

    后跟引号的反斜杠只是将引号插入字符串中。

        6
  •  2
  •   avpaderno    15 年前

    如果字符串是文字字符串,则可以使用转义字符在字符串中添加引号。

    NSString *string = @"16\"";
    
        7
  •  0
  •   AtulParmar    6 年前

    Swift 5,Xcode 10.2

    let myText = #"This is a quotation mark: ""#
    print(myText)
    

    输出: