Skip to content

Instantly share code, notes, and snippets.

@Pakkapon
Last active February 18, 2020 03:09
Show Gist options
  • Select an option

  • Save Pakkapon/b817bc9f657c4d1dc923d7d46a730a12 to your computer and use it in GitHub Desktop.

Select an option

Save Pakkapon/b817bc9f657c4d1dc923d7d46a730a12 to your computer and use it in GitHub Desktop.
// this method for trim text that have a lot of whitespace and newline to show just only max 2 lines and remove leading and traling of whitespace and new lines
func trimJustTwoUnderlines() -> String {
let characters = Array(self)
var newText: [String.Element] = []
var tempLineCounts: Int = 0
if characters.count > 1 {
for index in 0...characters.count-1 {
if characters[index].isNewline == false || characters[index].isWhitespace == false {
newText.append(characters[index])
tempLineCounts = 0
} else {
if tempLineCounts < 2 {
newText.append(characters[index])
tempLineCounts = tempLineCounts+1
}
}
}
return String(newText).trimmingCharacters(in: .whitespacesAndNewlines)
}
return self
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment