Last active
February 18, 2020 03:09
-
-
Save Pakkapon/b817bc9f657c4d1dc923d7d46a730a12 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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