Created
August 28, 2016 07:26
-
-
Save ethauvin/7ed3c3ef3b56e47b27dbf6153da0d7d9 to your computer and use it in GitHub Desktop.
Pluralize a string
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
fun String.plural(size: Int): String { | |
val consonants = "bcdfghjklmnpqrstvwxz" | |
if (size > 1 && this.length > 2) { | |
if ((this.endsWith("o", true) || this.endsWith("s", true)) && | |
consonants.contains(this[this.length - 2], true)) { | |
return this + "es" | |
} | |
if (this.endsWith("y", true) && | |
consonants.contains(this[this.length - 2], true)) { | |
return this + "ies" | |
} | |
} | |
return this + "s" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment