我有一个像这样的字符串:
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"
答案 0 :(得分:6)
这是怎么回事?
var aString = "This is a string \n\n\n This is the second line of the string\n\n"
// trim the string
aString.trimmingCharacters(in: CharacterSet.newlines)
// replace occurences within the string
while let rangeToReplace = aString.range(of: "\n\n") {
aString.replaceSubrange(rangeToReplace, with: "\n")
}
答案 1 :(得分:2)
您的代码的基本思想是用单个/ n替换所有double / n。
var aString = "This is my string"
var newString = aString.replacingOccurrences(of: "/n/n", with: "/n")
答案 2 :(得分:0)
试试这个
cf.input :device_identifier, input_html: { readonly: !cf.object.new_record? }