简体   繁体   English

包含特殊字符的Swift字符串

[英]Swift String including Special Characters

I have a user enter a multi string in an NSTextView. 我有一个用户在NSTextView中输入一个多字符串。

var textViewString = textView.textStorage?.string

Printing the string ( print(textViewString) ), I get a multi-line string, for example: 打印字符串( print(textViewString) ),我得到多行字符串,例如:

hello this is line 1 

and this is line 2

I want a swift string representation that includes the new line characters. 我想要一个包含换行符的快速字符串表示形式。 For example, I want print(textStringFlat) to print: 例如,我要print(textStringFlat)打印:

hello this is line 1\n\nand this is line 2

What do I need to do to textViewString to expose the special characters? 我需要对textViewString进行公开以显示特殊字符吗?

如果只想用文字字符\\n替换换行符,则使用:

let escapedText = someText.replacingOccurrences(of: "\n", with: "\\n")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM