Java Utililty Methods String Underscore

List of utility methods to do String Underscore

Description

The list of methods to do String Underscore are organized into topic(s).

Method

StringtoUnderscoreName(String name)
Convert a name in camelCase to an underscored name in lower case.
if (name == null)
    return null;
String filteredName = name;
if (filteredName.indexOf("_") >= 0 && filteredName.equals(filteredName.toUpperCase())) {
    filteredName = filteredName.toLowerCase();
if (filteredName.indexOf("_") == -1 && filteredName.equals(filteredName.toUpperCase())) {
    filteredName = filteredName.toLowerCase();
...
StringtoUnderscores(String s)
to Underscores
if (s == null) {
    return null;
if (s.equals("")) {
    return "";
StringBuffer sb = new StringBuffer(s);
char c;
...
StringtoUnderscoreSeparated(String name)
to Underscore Separated
if (name == null) {
    return name;
return name.replaceAll("([a-z])([A-Z])", "$1_$2").toLowerCase();
Stringunderscore(final String s, final Locale locale)
Converts the given string to 'underscore' style.
final String[] words = splitCamelCase(s);
for (int i = 0; i < words.length; ++i) {
    words[i] = words[i].toLowerCase(locale);
return join(words, '_');
Stringunderscore(String camel)
Converts a CamelCase string to underscores: "AliceInWonderLand" becomes: "alice_in_wonderland"
List<Integer> upper = new ArrayList<Integer>();
byte[] bytes = camel.getBytes();
for (int i = 0; i < bytes.length; i++) {
    byte b = bytes[i];
    if (b < 97 || b > 122) {
        upper.add(i);
StringBuffer b = new StringBuffer(camel);
for (int i = upper.size() - 1; i >= 0; i--) {
    Integer index = upper.get(i);
    if (index != 0)
        b.insert(index, "_");
return b.toString().toLowerCase();
Stringunderscore(String camelCaseWord)
Makes an underscored form from the expression in the string.
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();
...
Stringunderscore(String field)
underscore
return field.replace(' ', '_').replace('.', '_');
Stringunderscore(String phase)
underscore is the reverse of camelize method.
if (phase == null || "".equals(phase))
    return phase;
phase = phase.replace('-', '_');
StringBuilder sb = new StringBuilder();
int total = phase.length();
for (int i = 0; i < total; i++) {
    char c = phase.charAt(i);
    if (i == 0) {
...
Stringunderscore(String str)
underscore
if (str == null)
    return null;
return str.replaceAll("([A-Z]+)([A-Z][a-z])", "$1_$2").replaceAll("([a-z])([A-Z])", "$1_$2")
        .replaceAll("-", "_").toLowerCase();
Stringunderscore(String word)
underscore
String firstPattern = "([A-Z]+)([A-Z][a-z])";
String secondPattern = "([a-z\\d])([A-Z])";
String replacementPattern = "$1_$2";
word = word.replaceAll("\\.", "/"); 
word = word.replaceAll("::", "/"); 
word = word.replaceAll("\\$", "__"); 
word = word.replaceAll(firstPattern, replacementPattern); 
word = word.replaceAll(secondPattern, replacementPattern);
...