Java String Underscore underscore(String word)

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

Description

underscore

License

Open Source License

Declaration

public static String underscore(String word) 

Method Source Code

//package com.java2s;
/*//from   w ww.ja va  2s .  co  m
 * Copyright 2008-2012 Red Hat, Inc, and individual contributors.
 * 
 * This 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 software 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.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

public class Main {
    public static String underscore(String word) {
        String firstPattern = "([A-Z]+)([A-Z][a-z])";
        String secondPattern = "([a-z\\d])([A-Z])";
        String replacementPattern = "$1_$2";
        word = word.replaceAll("\\.", "/"); // replace package separator with
                                            // slash
        word = word.replaceAll("::", "/"); // replace package separator with
                                           // slash
        word = word.replaceAll("\\$", "__"); // replace $ with two underscores
                                             // for inner classes
        word = word.replaceAll(firstPattern, replacementPattern); // replace
                                                                  // capital
                                                                  // letter
                                                                  // with
                                                                  // _ plus
                                                                  // lowercase
                                                                  // letter
        word = word.replaceAll(secondPattern, replacementPattern);
        word = word.replace('-', '_');
        word = word.toLowerCase();
        return word;
    }
}

Related

  1. underscore(String camel)
  2. underscore(String camelCaseWord)
  3. underscore(String field)
  4. underscore(String phase)
  5. underscore(String str)
  6. underscoreAllWhitespace(String str)
  7. underscoreAndLowercase(String s)
  8. underScoreBreakToCaseBreak (final String s)
  9. underscoreCase(String s)