【发布时间】:2021-02-01 10:35:11
【问题描述】:
我将用户输入作为字符串保存到 db,我想删除每行的所有空格。
用户输入:
Hi!
My name is:
Bob
I am from the USA.
我想删除“Bob”之间的空格,所以结果是:
Hi!
My name is:
Bob
I am from the USA.
我正在尝试使用以下代码来实现
let regex = try! NSRegularExpression(pattern: "\n[\\s]+", options: .caseInsensitive)
a = regex.stringByReplacingMatches(in: a, options: [], range: NSRange(0..<a.utf16.count), withTemplate: "\n")
但是这段代码替换了多个新行“\n”,我不想这样做。
运行上述代码后:"1\n\n\n 2" -> "1\n2"。我需要的结果:"1\n\n\n2"(只删除空格,不删除新行)。
【问题讨论】:
标签: swift regex nsregularexpression