我让用户在 NSTextView 中输入多个字符串。
var textViewString = textView.textStorage?.string
打印字符串 ( print(textViewString)
),我得到一个多行字符串,例如:
hello this is line 1
and this is line 2
我想要一个包含换行符的快速字符串表示。例如,我希望 print(textStringFlat)
打印:
hello this is line 1\n\nand this is line 2
我需要对 textViewString
做什么才能显示特殊字符?
最佳答案
如果您只想用文字字符 \
和 n
替换换行符,请使用:
let escapedText = someText.replacingOccurrences(of: "\n", with: "\\n")
关于包含特殊字符的 Swift 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51995073/