Java String Pluralize pluralize(String word)

Here you can find the source of pluralize(String word)

Description

pluralize

License

Open Source License

Return

the plural of word. This is done by applying a few rules. These cover most (but not all) cases: 1. If the word ends in s, ss, x, o, or ch, append es 2. If the word ends in a consonant followed by y, drop the y and add ies 3. Append an s and call it a day. The ultimate references is at http://en.wikipedia.org/wiki/English_plural

Declaration

public static String pluralize(String word) 

Method Source Code

//package com.java2s;
/*/*  w  ww .  j  a va2  s. c  om*/
 * RHQ Management Platform
 * Copyright (C) 2005-2008 Red Hat, Inc.
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2, as
 * published by the Free Software Foundation, and/or the GNU Lesser
 * General Public License, version 2.1, also as published by the Free
 * Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License and the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License
 * and the GNU Lesser General Public License along with this program;
 * if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

public class Main {
    /**
     * @return the plural of word. This is done by applying a few rules. These cover most (but not all) cases: 1. If the
     *         word ends in s, ss, x, o, or ch, append es 2. If the word ends in a consonant followed by y, drop the y
     *         and add ies 3. Append an s and call it a day. The ultimate references is at
     *         http://en.wikipedia.org/wiki/English_plural
     */
    public static String pluralize(String word) {
        if (word.endsWith("s") || word.endsWith("x") || word.endsWith("o") || word.endsWith("ch")) {
            return word + "es";
        }

        if (word.endsWith("y")) {
            // Odd case to avoid StringIndexOutOfBounds later
            if (word.length() == 1) {
                return word;
            }

            // Check next-to-last letter
            char next2last = word.charAt(word.length() - 2);
            if ((next2last != 'a') && (next2last != 'e') && (next2last != 'i') && (next2last != 'o')
                    && (next2last != 'u') && (next2last != 'y')) {
                return word.substring(0, word.length() - 1) + "ies";
            }
        }

        return word + "s";
    }
}

Related

  1. pluralize(String s)
  2. pluralize(String singular, long number)
  3. pluralize(String singularNoun)
  4. pluralize(String singularNoun)
  5. pluralize(String suggestion)
  6. pluralize(String word)
  7. pluralize(String word, int count)
  8. pluralize(String x, Integer y)
  9. pluralizeName(String name)