2

Possible Duplicate:
Plural form of a word

Is there some existing class or library for adding "s" to a String if I pass it a number that's not 1? Basically, I have a cat. If I have 1 cat, I need the String "cat". If I have 2 cats, I need the String "cats". It's a simple thing to do myself, but after I did it, I thought there's probably already a library I could import for it. However, as you can see, I am having difficulty putting this into words to Google a name for the package, if it exists. :P It's just that I write this function all the time, I'm wondering if it exists already.

Community
  • 1
  • 1
SnoFox
  • 23
  • 1
  • 4
  • You want an API that pluralizes? – Jon Lin Aug 02 '12 at 03:45
  • @JonLin yeah, pretty much. XD Is there a pluralizing package for Java? Really I'm doing just time words (seconds, minutes, hours), but if there's one for cacti too, that'd be interesting. – SnoFox Aug 02 '12 at 03:49

2 Answers2

1

Pluralizing is probably simple enough, with the exception of the list of words like "cactus" or "tooth" that are somewhat special cases, that you don't need an entire API or you can implement yourself. If you really don't want to have a stab at it, there are things like the Inflector library that can do it for you. There are probably others, if you search for "java pluralize".

Javadocs for Inflector here: http://www.atteo.org/static/evo-inflector/apidocs/index.html

Max
  • 1,107
  • 12
  • 24
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
0

Really I'm doing just time words (seconds, minutes, hours) ...

If you have a small / fixed set of words, it is probably simpler to use a static lookup table or something like that. A general solution is too heavy-weight, IMO.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • True. I'll probably find a better package for my other projects so I can stop having output like "tooths", where I wasn't paying attention. For the small set of words, I'll continue using a simple function. – SnoFox Aug 02 '12 at 03:57