Java String Underscore underscore(String camelCaseWord)

Here you can find the source of underscore(String camelCaseWord)

Description

Makes an underscored form from the expression in the string.

License

Open Source License

Parameter

Parameter Description
camelCaseWord the camel-cased word that is to be converted;

Return

a lower-cased version of the input, with separate words delimited by the underscore character.

Declaration

public static String underscore(String camelCaseWord) 

Method Source Code

//package com.java2s;
/**/*  www  .j av a 2s .  com*/
 * JLibs: Common Utilities for Java
 * Copyright (C) 2009  Santhosh Kumar T <santhosh.tekuri@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 */

public class Main {
    /**
     * Makes an underscored form from the expression in the string.
     * <p>
     * Examples:
     * <pre class="prettyprint">
     * underscore("activeRecord")     // "active_record"
     * underscore("ActiveRecord")     // "active_record"
     * underscore("firstName")        // "first_name"
     * underscore("FirstName")        // "first_name"
     * underscore("name")             // "name"
     * </pre>
     *
     * @param camelCaseWord the camel-cased word that is to be converted;
     * @return a lower-cased version of the input, with separate words delimited by the underscore character.
     */
    public static String underscore(String camelCaseWord) {
        if (camelCaseWord == null)
            return null;
        camelCaseWord = camelCaseWord.trim();
        if (camelCaseWord.length() == 0)
            return "";
        camelCaseWord = camelCaseWord.replaceAll("([A-Z]+)([A-Z][a-z])",
                "$1_$2");
        camelCaseWord = camelCaseWord.replaceAll("([a-z\\d])([A-Z])",
                "$1_$2");
        return camelCaseWord.toLowerCase();
    }
}

Related

  1. toUnderscoreName(String name)
  2. toUnderscores(String s)
  3. toUnderscoreSeparated(String name)
  4. underscore(final String s, final Locale locale)
  5. underscore(String camel)
  6. underscore(String field)
  7. underscore(String phase)
  8. underscore(String str)
  9. underscore(String word)