Java String Underscore toUnderscoreCase(String name)

Here you can find the source of toUnderscoreCase(String name)

Description

to Underscore Case

License

Apache License

Declaration

public static StringBuilder toUnderscoreCase(String name) 

Method Source Code

//package com.java2s;
//Licensed under the Apache License, Version 2.0 (the "License");

public class Main {
    public static StringBuilder toUnderscoreCase(String name) {
        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)
                    appendUnderscore = true;

                continue;
            }/*from  www  .  j ava 2s  . c o m*/

            if (appendUnderscore)
                buffer.append('_');

            if (c > 96 && c < 123) {
                buffer.append(c);
                toLower = true;
            } else if (c > 64 && c < 91) {
                if (toLower) {
                    // avoid duplicate underscore
                    if (!appendUnderscore)
                        buffer.append('_');
                    toLower = false;
                }
                buffer.append((char) (c + 32));
            } else {
                buffer.append(c);
                toLower = false;
            }
            appendUnderscore = false;
        }
        return buffer;
    }
}

Related

  1. toUnderscore(final String input, final boolean plural)
  2. toUnderscore(String s)
  3. toUnderscore(String text)
  4. toUnderscore(String v)
  5. toUnderscoreCase(final String s)
  6. toUnderScoreCase(String s)
  7. toUnderScoreCase(String s)
  8. toUnderscored(String name)
  9. toUnderscoredLowercase(String text)