Here you can find the source of underscore(String camelCaseWord)
Parameter | Description |
---|---|
camelCaseWord | the camel-cased word that is to be converted; |
public static String underscore(String camelCaseWord)
//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(); } }