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

StringtoUnderscore(final String input, final boolean plural)
to Underscore
StringBuilder out = new StringBuilder();
for (int i = 0; i < input.length(); i++) {
    char c = input.charAt(i);
    if (Character.isUpperCase(c)) {
        if (i > 0) {
            out.append("_");
        out.append(Character.toLowerCase(c));
...
StringtoUnderscore(String s)
Converts camelCaseVersusC to camel_case_versus_c
StringBuffer buf = new StringBuffer();
char[] ch = s.toCharArray();
for (int i = 0; i < ch.length; ++i) {
    if (Character.isUpperCase(ch[i])) {
        buf.append('_');
        buf.append(Character.toLowerCase(ch[i]));
    } else {
        buf.append(ch[i]);
...
StringtoUnderscore(String text)
to Underscore
return text.replaceAll("[ \\.-]+", "_"); 
StringtoUnderscore(String v)
userName -> user_name Password -> _password
StringBuilder sb = new StringBuilder();
char[] chars = v.toCharArray();
for (char ch : chars) {
    if (ch >= 65 && ch <= 90) {
        sb.append("_");
    sb.append(Character.toLowerCase(ch));
return sb.toString();
StringtoUnderscoreCase(final String s)
Converts an optionally camel-cased character sequence (e.g.
final char[] chars = s.toCharArray();
final StringBuilder sb = new StringBuilder();
char previousChar = 0;
for (final char aChar : chars) {
    if (Character.isUpperCase(aChar)) {
        if (previousChar != 0) {
            sb.append('_');
        sb.append(Character.toLowerCase(aChar));
    } else {
        sb.append(aChar);
    previousChar = aChar;
return sb.toString();
StringBuildertoUnderscoreCase(String name)
to Underscore Case
StringBuilder buffer = new StringBuilder();
boolean toLower = false, appendUnderscore = false;
for (int i = 0, len = name.length(); i < len;) {
    char c = name.charAt(i++);
    if (c == '_') {
        if (i == len)
            break;
        if (buffer.length() != 0)
...
StringtoUnderScoreCase(String s)
to Under Score Case
if (s == null) {
    return null;
return s.replaceAll(String.format("%s|%s|%s", "(?<=[A-Z])(?=[A-Z][a-z])", "(?<=[^A-Z])(?=[A-Z])",
        "(?<=[A-Za-z])(?=[^A-Za-z])"), "_").toLowerCase();
StringtoUnderScoreCase(String s)
to Under Score Case
if (s == null) {
    return null;
StringBuilder sb = new StringBuilder();
boolean upperCase = false;
for (int i = 0; i < s.length(); i++) {
    char c = s.charAt(i);
    boolean nextUpperCase = true;
...
StringtoUnderscored(String name)
to Underscored
return toEnglish(name).replaceAll(" ", "_").toLowerCase();
StringtoUnderscoredLowercase(String text)
to Underscored Lowercase
String result = text.replaceAll("(?<=[a-z0-9])[A-Z]|(?<=[a-zA-Z])[0-9]|(?<=[A-Z])[A-Z](?=[a-z])", "_$0");
result = result.toLowerCase();
return result;