我试图打印它,但它只是通过,因为它是一个转义字符。 例如输出应该如下。
\correct
提前致谢
答案 0 :(得分:46)
为此以及将来参考:
\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.
答案 1 :(得分:4)
var s1: String = "I love my "
let s2: String = "country"
s1 += "\"\(s2)\""
print(s1)
它将打印我爱我的“国家”
答案 2 :(得分:3)
反斜杠字符\
在字符串中使用时充当转义字符。这意味着您可以在字符串中使用双引号,方法是使用\
预先挂起它们。这同样适用于反斜杠字符本身,也就是说println("\\")
将导致仅打印\
。
答案 3 :(得分:1)
对 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