基于某些集合大小处理单数/复数单词的有效方法 [英] Effective way to handle singular/plural word based on some collection size

查看:104
本文介绍了基于某些集合大小处理单数/复数单词的有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作项目中有很多实例需要在一个句子中显示某些集合的大小。例如,如果集合的大小为5,则表示5个用户。如果它的大小为1或0,则表示1个用户或0个用户。现在,我正在用if-else语句来确定是否打印s,这很乏味。

There are many instances in my work projects where I need to display the size of some collection in a sentence. For example, if the collection's size is 5, it will say "5 users". If it is size of 1 or 0, it will say "1 user" or "0 user". Right now, I'm doing it with if-else statements to determine whether to print the "s" or not, which is tedious.

我想知道是否有一个开源JSP自定义标记库,允许我完成此任务。我知道我自己可以写一个...基本上,它会有2个参数,如下所示:< lib:display word =usercollection =userList/> 。根据集合大小,它将决定是否附加s。但是,这个实现不会太强大,因为我还需要处理ies,有些词不使用任何这些。因此,我希望有一个更强大的库可以立即使用,而不是创建一个半生不熟的工具。在这种情况下,我并不太担心用is / are加上前缀。

I'm wondering if there's an open source JSP custom tag library that allows me to accomplish this. I know I can write one myself... basically, it will have 2 parameters like this: <lib:display word="user" collection="userList" />. Depending on the collection size, it will determine whether to append an "s" or not. But then, this implementation is not going to be too robust because I also need to handle "ies" and some words don't use any of those. So, instead of creating a half-baked tool, I'm hoping there's a more robust library I could utilize right away. I'm not too worried about prefixing the word with is/are in this case.

顺便说一下,我使用Java。

I use Java, by the way.

非常感谢。

推荐答案

看看 inflector ,一个允许你做 Noun.pluralOf(user)或<$的java项目c $ c> Noun.pluralOf(user,userList.size()),它处理一堆变化和异常情况(人 - >人,面包 - >面包等) ,以及让您在必要时定义自定义映射规则。

Take a look at inflector, a java project which lets you do Noun.pluralOf("user"), or Noun.pluralOf("user", userList.size()), and which handles a bunch of variations and unusual cases (person->people, loaf->loaves, etc.), as well as letting you define custom mapping rules when necessary.

这篇关于基于某些集合大小处理单数/复数单词的有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆