Skip to content

Instantly share code, notes, and snippets.

@ethauvin
Created August 28, 2016 07:26
Show Gist options
  • Save ethauvin/7ed3c3ef3b56e47b27dbf6153da0d7d9 to your computer and use it in GitHub Desktop.
Save ethauvin/7ed3c3ef3b56e47b27dbf6153da0d7d9 to your computer and use it in GitHub Desktop.
Pluralize a string
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