我试图打印它,但它只是通过,因为它是一个逃脱的角色。例如输出应如下。
\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.
var s1: String = "I love my "
let s2: String = "country"
s1 += "\"\(s2)\""
print(s1)
它将打印我爱我的“国家”
当在字符串中使用时,反斜杠字符\
充当转义字符。这意味着您可以在字符串中使用双引号,方法是使用\
预先挂起它们。这同样适用于反斜杠字符本身,也就是说println("\\")
将导致仅打印\
。
对Swift 5,Xcode 10.2使用以下代码
let myText = #"This is a Backslash: \"#
print(myText)
输出:
这是一个反斜杠:
现在不需要在swift 5中添加双斜杠来使用单斜杠,即使现在在某些字符之前需要斜杠,例如单引号,双引号等。
有关swift 5的最新更新,请参阅此帖子
https://www.hackingwithswift.com/articles/126/whats-new-in-swift-5-0