Lately, I’ve been working on a project that requires some basic MLP text processing to be performed on device. A component of our text processing involved removing any trailing punctuation, no matter the language.
I’ve found the below String Extension is a good balance between effectiveness, performance, and readability. Especially if you are collecting input from users prone to excessive punctuation.
This file contains 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
extension String { | |
func trimTrailingPunctuation() -> String { | |
return self.trimmingCharacters(in: .whitespacesAndNewlines) | |
.trimmingCharacters(in: .punctuationCharacters) | |
.trimmingCharacters(in: .whitespacesAndNewlines) | |
} | |
} |
This file contains 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
let example1 = "How are you???".trimTrailingPunctuation() | |
>>> How are you | |
let example2 = "Hi!!!!".trimTrailingPunctuation() | |
>>> Hi | |
let example3 = "Act limited time offer now!".trimTrailingPunctuation() | |
>>> Act limited time offer now |