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

StringunderscoreAllWhitespace(String str)
Change to underscore all whitespace from the given String: leading, trailing, and inbetween characters.
if (str == null || str.length() == 0) {
    return str;
StringBuilder sb = new StringBuilder(str);
int index = 0;
while (sb.length() > index) {
    if (Character.isWhitespace(sb.charAt(index))) {
        sb.replace(index, index + 1, "_");
...
StringunderscoreAndLowercase(String s)
underscore And Lowercase
return s.replaceAll(String.format("%s|%s", "(?<=[A-Z])(?=[A-Z][a-z])", "(?<=[^A-Z])(?=[A-Z])"), "_")
        .toLowerCase();
StringunderScoreBreakToCaseBreak(final String s)
under Score Break To Case Break
final StringBuilder sb = new StringBuilder();
final char[] chars = s.toCharArray();
boolean nextShouldBeUpperCase = true;
for (final char c : chars) {
    if (c == '_') {
        nextShouldBeUpperCase = true;
    } else if (c == '$' || !Character.isJavaIdentifierPart(c)) {
        sb.append('_');
...
StringunderscoreCase(String s)
underscore Case
if (s.isEmpty()) {
    return s;
StringBuilder sb = new StringBuilder();
char[] chars = s.toCharArray();
sb.append(chars[0]);
for (int i = 1; i < chars.length; i++) {
    char ch = chars[i];
...
StringunderScoreCase2CamelCase(String str)
under Score Case Camel Case
if (!str.contains("_"))
    return str;
StringBuilder sb = new StringBuilder();
char[] chars = str.toCharArray();
boolean hitUnderScore = false;
sb.append(chars[0]);
for (int i = 1; i < chars.length; i++) {
    char c = chars[i];
...
Stringunderscored(String string)
Convert from CamelCase to underscored: MyModel -> my_model.
return separated(string, '_');
StringunderscoredToCamel(String string)
underscored To Camel
String[] a = string.split("_");
for (int i = 0, n = a.length; i < n; i++) {
    String s = a[i].trim();
    a[i] = capitalize(a[i]);
return implode(a, "");
StringunderscoreName(String camelCaseName)
underscore Name
StringBuilder result = new StringBuilder();
if (camelCaseName != null && camelCaseName.length() > 0) {
    result.append(camelCaseName.substring(0, 1).toLowerCase());
    for (int i = 1; i < camelCaseName.length(); i++) {
        char ch = camelCaseName.charAt(i);
        if (Character.isUpperCase(ch)) {
            result.append("_");
            result.append(Character.toLowerCase(ch));
...
StringunderscoreName(String name)
underscore Name
return name.substring(0, 1).toLowerCase() + name.substring(1).replaceAll("([A-Z])", "_$1").toLowerCase();
StringunderscoreSpaces(String input)
underscore Spaces
if (input == null)
    return null;
String tmp = input.trim().replaceAll("\\s+", "_");
tmp = tmp.replaceAll("_+", "_");
return tmp;