ios - 从字符串中删除换行符

标签 ios swift

我有这样一个字符串:

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/

相关文章:

ios - 我想使用 swiftdata (ryanfowler) 在 swift 2 中使用 sqlite 但有 120 个错误,我该怎么办?

ios - 我正在尝试在默认网络浏览器中打开一个 url。但它没有回应。它不会打开默认的网络浏览器

iphone - 扩展多行 UILabel

Swift For In Loop 遍历一组自定义类不起作用

ios - 自定义导航 Controller 以显示固定顶部菜单

ios - 左右两侧的 UIButton 边框

ios - 如何为 iPad Retina 示例 (GLGravity) 创建 1536x2048 帧缓冲区

ios - UICollectionView - 水平排列单元格

ios - 未调用 Swift 类函数

ios - 如何限制按钮的音量(Swift)