我尝试打印它,但它被跳过了,因为它是一个转义字符。例如,输出应该如下所示。
\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.
使用以下代码适用于 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
var s1: String = "I love my "
let s2: String = "country"
s1 += "\"\(s2)\""
print(s1)
它将打印出“我爱我的 '国家'”
当字符串中使用反斜杠字符\
时,它会充当转义字符。这意味着您可以在字符串中使用双引号,只需用\
进行前置即可。同样地,反斜杠字符本身也适用于此规则,也就是说,println("\\")
的结果只会打印一个\
。
"ab\\cd"
将得到"ab\cd"
。 - Eric Aya