(Swift)如何在string中打印“\”字符?

我试图打印它,但它只是通过,因为它是一个逃脱的字符。 例如输出应该如下。

\correct 

提前致谢

对于这一点也是未来的参考:

 \0 – Null character (that is a zero after the slash) \\ – Backslash itself. Since the backslash is used to escape other characters, it needs a special escape to actually print itself. \t – Horizontal tab \n – Line Feed \r – Carriage Return \” – Double quote. Since the quotes denote a String literal, this is necessary if you actually want to print one. \' – Single Quote. Similar reason to above. 

在string中使用时,反斜杠\作为转义字符。 这意味着您可以在string中使用(例如双引号),方法是用\预先等待它们。 这同样适用于反斜杠字符本身,也就是说println("\\")将被打印出来。

 var s1: String = "I love my " let s2: String = "country" s1 += "\"\(s2)\"" print(s1) 

它会打印我爱我的“国家”

Interesting Posts