Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove whitespace and multiple line from string?

Tags:

string

ios

swift

I have a string , like this:

"  \n assddd\n\n\n\n\n adjf fff dd \n\n\n\n       tjhfhdf \n      "

I want the result is:

"assddd\nadjffffdd\ntjhfhdf". 

1: I used trimmingCharacters to remove the beginning and the ending:

let title = "  \n assddd\n\n\n\n\n adjf fff dd \n\n\n\n       tjhfhdf \n".trimmingCharacters(in: .whitespacesAndNewlines)

2: remove whitespaces

let result = title.replacingOccurrences(of: " ", with: "")

but, how to keep the first "\n" between the character and remove other "\n" ?

like image 242
Longshihua Avatar asked Jan 03 '23 02:01

Longshihua


2 Answers

You can find two or more consecutive newline characters with a regular expression, and replace them with a single newline character. Example:

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"

Applied to your case:

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"
like image 162
Martin R Avatar answered Jan 05 '23 15:01

Martin R


Starting with your sample text, we can trim the ends:

let sample = "  \n assddd\n\n\n\n\n adjf fff dd \n\n\n\n       tjhfhdf \n      "
let trimmedEnds = sample.trimmingCharacters(in: .whitespacesAndNewlines)

If you just want to remove space and compress newlines:

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

or using a regular expression for the spaces:

let noHorizSpace = trimmedEnds.replacingOccurrences(of: " +", with: "", options: .regularExpression)
let singleVertSpace = noHorizSpace.replacingOccurrences(of: "\\n{2,}", with: "\n", options: .regularExpression)

But (for fun?) what about all Unicode horizontal (spaces, tabs, etc.) and vertical (newlines, form feeds, paragraph separators, etc.)? For that there are the RE patterns \h and \v:

let noHorizSpace = trimmedEnds.replacingOccurrences(of: "\\h+", with: "", options: .regularExpression)
let singleVertSpace = noHorizSpace.replacingOccurrences(of: "\\v+", with: "\n", options: .regularExpression)

You can solve this with a single regular expression, but it is better to heed the advice in ICU RE User Guide and use multiple simpler REs.

like image 38
CRD Avatar answered Jan 05 '23 15:01

CRD