Java String Pluralize pluralize(String input)

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

Description

Pluralize a property name, which is smart about things like nouns ending in 's' or 'y'.

License

Open Source License

Declaration

public static String pluralize(String input) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /** Pluralize a property name, which is smart about things like nouns ending in 's' or 'y'.
     * *//*from ww w . j  a  va 2  s  . c  o m*/
    public static String pluralize(String input) {
        //  See http://firstschoolyears.com/literacy/word/other/plurals/resources/rules.htm for a decent list of rules
        String result;
        if (input.endsWith("ife")) {
            return input.substring(0, input.length() - 3) + "ives";
        } else if (input.endsWith("s") || input.endsWith("sh") || input.endsWith("ch") || input.endsWith("x")
                || input.endsWith("z")) {
            result = input.concat("es");
        } else if (input.endsWith("f") && !input.substring(input.length() - 2, 1).matches("[efo]")) {
            return input.substring(0, input.length() - 1) + "ves";
        } else if (input.endsWith("o")) {
            return input + "es";
        } else if (input.endsWith("y") && !isVowel(input.charAt(input.length() - 2))) {
            result = input.substring(0, input.length() - 1).concat("ies");
        } else {
            result = input.concat("s");
        }
        return result;
    }

    private static boolean isVowel(char c) {
        char lower = Character.toLowerCase(c);
        return lower == 'a' || lower == 'e' || lower == 'i' || lower == 'o' || lower == 'u';
    }
}

Related

  1. pluralize(int value, CharSequence singularForm, CharSequence pluralForm)
  2. pluralize(long count, String singularFormat, String pluralFormat)
  3. pluralize(long strictlyPositiveCount, String singular, String plural)
  4. pluralize(long val, String singular, String plural)
  5. pluralize(String base, int quantity)
  6. pluralize(String name)
  7. pluralize(String name)
  8. pluralize(String name)
  9. pluralize(String name)