Removing line breaks from string

what about this?

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")
}

The basic idea for your code would be replacing all double /n with a single /n.

var aString = "This is my string"
var newString = aString.replacingOccurrences(of: "\n\n", with: "\n")

Tags:

Ios

Swift