如何存储\">在Swift字符串中?

时间:2017-05-19 14:52:59

标签: swift special-characters swift-playground escapestring

我试图存储\">在一个字符串中。从理论上说," \\"应该打印\和" \""打印"。但是,当我尝试使用\在字符串中打印\时,输出是双正斜杠:

var str = "\">" 
// ">
var str2 = "\\"
// \\ 
var str3 = "\\\">"
// \\">
var str4 = "\\">"
// escapes after quotation mark (error)

3 个答案:

答案 0 :(得分:2)

您的str3是对的。例如:

print("\\\">")

打印所需的序列:

  

\">

您可能会将Swift Playground生成的转义输出与实际字符串混淆。如有疑问,请尝试控制台:)

答案 1 :(得分:2)

答案是Android Repository https://dl.google.com/android/repository/repository2-1.xml Android System Images https://dl.google.com/android/repository/sys-img/android/sys-img2-1.xml Android TV System Images https://dl.google.com/android/repository/sys-img/android-tv/sys-img2-1.xml Android Wear System Images https://dl.google.com/android/repository/sys-img/android-wear/sys-img2-1.xml Glass Development Kit, Google Inc. https://dl.google.com/android/repository/glass/addon2-1.xml Google API add-on System Images https://dl.google.com/android/repository/sys-img/google_apis/sys-img2-1.xml Google API with Playstore System Images https://dl.google.com/android/repository/sys-img/google_apis_playstore/sys-img2-1.xml Google Inc. https://dl.google.com/android/repository/addon2-1.xml Intel HAXM https://dl.google.com/android/repository/extras/intel/addon2-1.xml Offline Repo file:/C:/Program%20Files/Android/Android%20Studio/plugins/sdk-updates/offline-repo/offline-repo.xml 。例如见:

path
  

\
  “
  >

Xcode中的str3本身输出无关紧要,因为Xcode可以决定如何显示字符串。例如。它决定显示var str3 = "\\\">" str3.characters.forEach { print($0) } 的转义字符。 str3前显示转义,因为在Playground中,内部\的颜色已经与实际分隔字符串的"颜色不同:
enter image description here

答案 2 :(得分:1)

操场输出错误(或者我不明白)。 print()会为您提供正确的输出:

var str3 = "\\\">"  // Plaground output:  \\">
print(str3)

打印:\">