我想检测TextView
中的换行符,以将其替换为某些Unicode符号。有什么方法可以快速检测到换行吗?我想替换整个文本中的每个换行符。
答案 0 :(得分:1)
使用正则表达式匹配换行符,然后将其替换为所需的换行符。将"""
用于多行文字
let replacement = ","
let yourText = textView.text // your textview's text
let regex = try! NSRegularExpression(pattern: "[\n]", options: NSRegularExpression.Options.caseInsensitive)
let range = NSMakeRange(0, yourText.utf16.count)
let replacedString = regex.stringByReplacingMatches(in: yourText, options: [], range: range, withTemplate: replacement)
print(replacedString) // will add commas instead of line breaks