从字符串中删除换行符

2024-01-09

我有一个像这样的字符串:

var aString = "This is a string \n\n This is the second line of the string\n\n"

文本视图内部如下所示:

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")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从字符串中删除换行符 的相关文章

随机推荐