Java String Singularize singularise(String name)

Here you can find the source of singularise(String name)

Description

singularise

License

Open Source License

Declaration

public static String singularise(String name) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2013 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * /* w w w.ja v a2s .c  o m*/
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/

public class Main {
    public static String singularise(String name) {
        String result = name;
        if (seemsPluralised(name)) {
            String lower = name.toLowerCase();
            if (lower.endsWith("ies")) {
                // cities --> city
                result = name.substring(0, name.length() - 3) + "y";
            } else if (lower.endsWith("ches") || lower.endsWith("ses")) {
                // switches --> switch or buses --> bus
                result = name.substring(0, name.length() - 2);
            } else if (lower.endsWith("s")) {
                // customers --> customer
                result = name.substring(0, name.length() - 1);
            }
        }
        return result;
    }

    private static boolean seemsPluralised(String name) {
        name = name.toLowerCase();
        boolean pluralised = false;
        pluralised |= name.endsWith("es");
        pluralised |= name.endsWith("s");
        pluralised &= !(name.endsWith("ss") || name.endsWith("us"));
        return pluralised;
    }
}

Related

  1. singular(String plural)
  2. singularize(final String buffer)
  3. singularize(String value)
  4. singularOrPlural(int testValue, String singular, String plural)
  5. singularPlural(int n, String singular, String plural)