我有一个字符串,如下:
" \n assddd\n\n\n\n\n adjf fff dd \n\n\n\n tjhfhdf \n "
我希望结果是:
"assddd\nadjffffdd\ntjhfhdf".
1:我使用trimmingCharacters来删除开头和结尾:
let title = " \n assddd\n\n\n\n\n adjf fff dd \n\n\n\n tjhfhdf \n".trimmingCharacters(in: .whitespacesAndNewlines)
2:删除空格
let result = title.replacingOccurrences(of: " ", with: "")
但是,如何在角色之间保留第一个“\ n”并删除其他“\ n”?
答案 0 :(得分:4)
您可以找到两个或多个连续的换行符 一个正则表达式,并用一个换行符替换它们 字符。例如:
let s1 = "AA\n\nBB\nCC\n\n\n\n\nDD"
let s2 = s1.replacingOccurrences(of: "\\n{2,}", with: "\n", options: .regularExpression)
print(s1.debugDescription) // "AA\n\nBB\nCC\n\n\n\n\nDD"
print(s2.debugDescription) // "AA\nBB\nCC\nDD"
适用于您的案件:
let title = " \n assddd\n\n\n\n\n adjf fff dd \n\n\n\n tjhfhdf \n "
let result = title.trimmingCharacters(in: .whitespacesAndNewlines)
.replacingOccurrences(of: " ", with: "")
.replacingOccurrences(of: "\\n{2,}", with: "\n", options: .regularExpression)
print(result.debugDescription) // "assddd\nadjffffdd\ntjhfhdf"
答案 1 :(得分:1)
可能的解决方案:
let str = " \n assddd\n\n\n\n\n adjf fff dd \n\n\n\n tjhfhdf \n "
print("str: \(str)")
let str2 = str.replacingOccurrences(of: " ", with: "")
print("str2: \(str2)")
let lines = str2.components(separatedBy:"\n")
print("lines: \(lines)")
let linesFiltered = lines.filter({($0.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)).count > 0})
print("linesFiltered: \(linesFiltered)")
let linesTrimmed = linesFiltered.map({$0.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)})
print("linesTrimmed: \(linesTrimmed)")
let endStr = linesTrimmed.joined(separator: "\n")
print("endStr:\n\(endStr)")
print("endStr:\n\(endStr.debugDescription)")
这个想法:
删除所有空格,因为您不想要它们
获取由breakLine(“\ n”)分隔的所有线条因为如果重复需要重新组装它们并将它们放入阵列中
删除空行(只有空格和/或新行)
删除每行(修剪)之前和之后的空格/换行符
重写字符串
此输出:
str:
assddd
adjf fff dd
tjhfhdf
str2:
assddd
adjffffdd
tjhfhdf
lines: ["", "assddd", "", "", "", "", "adjffffdd", "", "", "", "tjhfhdf", ""]
linesFiltered: ["assddd", "adjffffdd", "tjhfhdf"]
linesTrimmed: ["assddd", "adjffffdd", "tjhfhdf"]
endStr:
assddd
adjffffdd
tjhfhdf
endStr:
"assddd\nadjffffdd\ntjhfhdf"
答案 2 :(得分:1)
正如我在评论中所说,使用常规表达,你可以这样做,
func stringByAdjustingString(text:String) ->String{
do{
let regex = try NSRegularExpression(pattern: "\\n+", options:[.dotMatchesLineSeparators])
let resultString = regex.stringByReplacingMatches(in: text, range: NSMakeRange(0, text.utf16.count), withTemplate: "\n")
return resultString.replacingOccurrences(of: " ", with: "").trimmingCharacters(in: .whitespacesAndNewlines)
}
catch{
return ""
}
}
输入:" \ n assddd \ n \ n \ n \ n \ n adjf fff dd \ n \ n \ n \ n tjhfhdf \ n"
输出" assddd \ nadjffffdd \ ntjhfhdf"
答案 3 :(得分:1)
It should help you
var str = " \n assddd\n\n\n\n\n adjf fff dd \n\n\n\n tjhfhdf \n "
str = str.trimmingCharacters(in: .whitespacesAndNewlines)
str = str.replacingOccurrences(of: " ", with: "")
//str = str.replacingOccurrences(of: "\n\n", with: "\n")
let array = str.components(separatedBy: "\n")
var finalArray = [String]()
for x in array {
if !x.isEmpty {
finalArray.append(x)
}
}
str = finalArray.joined(separator: "\n")
答案 4 :(得分:1)
从示例文本开始,我们可以修改结尾:
let sample = " \n assddd\n\n\n\n\n adjf fff dd \n\n\n\n tjhfhdf \n "
let trimmedEnds = sample.trimmingCharacters(in: .whitespacesAndNewlines)
如果您只想删除空格并压缩换行符:
let noHorizSpace = trimmedEnds.replacingOccurrences(of: " ", with: "") // remove all spaces
let singleVertSpace = noHorizSpace.replacingOccurrences(of: "\\n{2,}", with: "\n", options: .regularExpression) // squash runs of two or more newlines
或使用空格的正则表达式:
let noHorizSpace = trimmedEnds.replacingOccurrences(of: " +", with: "", options: .regularExpression)
let singleVertSpace = noHorizSpace.replacingOccurrences(of: "\\n{2,}", with: "\n", options: .regularExpression)
但是(为了好玩?)所有Unicode水平(空格,制表符等)和垂直(换行符,换页符,段落分隔符等)怎么样?为此,有RE模式\h
和\v
:
let noHorizSpace = trimmedEnds.replacingOccurrences(of: "\\h+", with: "", options: .regularExpression)
let singleVertSpace = noHorizSpace.replacingOccurrences(of: "\\v+", with: "\n", options: .regularExpression)
你可以用一个正则表达式来解决这个问题,但最好注意ICU RE User Guide中的建议并使用多个更简单的RE。
答案 5 :(得分:0)
var titleTrimmedLines = [String]()
let title = " \n assddd\n\n\n\n\n adjf fff dd \n\n\n\n tjhfhdf \n "
title(separatedBy: CharacterSet.newlines).forEach { titleTrimmedLines.append($0.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines)) }
let result = titleTrimmedLines.joined()
答案 6 :(得分:-1)
您可以使用以下方法删除字符串中的空格
yourString.trimmingCharacters(in: .whitespaces)
或者如果你想要删除空格和行
yourString.trimmingCharacters(in: .whitespacesAndNewlines)
我希望它有所帮助!