我有这样一个字符串:
var aString = "This is a string \n\n This is the second line of the string\n\n"
在 textview 中看起来像这样:
This is a string
This is the second line of the string
// 2 extra lines of unnecessary white space
但我希望它看起来像这样:
This is a string
This is the second line of the string
我想从字符串末尾删除所有“\n”,并删除它重复的\n,这样中间就没有空格了。
理想情况下,我猜最终结果应该是这样的:
var aString = "This is a string \n This is the second line of the string"
最佳答案
您的代码的基本思想是将所有双/n 替换为单个/n。
var aString = "This is my string"
var newString = aString.replacingOccurrences(of: "\n\n", with: "\n")
关于ios - 从字符串中删除换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43073073/