Java String Underscore toUnderScoreCase(String s)

Here you can find the source of toUnderScoreCase(String s)

Description

to Under Score Case

License

Apache License

Declaration

public static String toUnderScoreCase(String s) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    private static final char SEPARATOR = '_';

    public static String toUnderScoreCase(String s) {
        if (s == null) {
            return null;
        }/*from w w w. j a v  a 2  s .c  o  m*/

        StringBuilder sb = new StringBuilder();
        boolean upperCase = false;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);

            boolean nextUpperCase = true;

            if (i < (s.length() - 1)) {
                nextUpperCase = Character.isUpperCase(s.charAt(i + 1));
            }

            if ((i > 0) && Character.isUpperCase(c)) {
                if (!upperCase || !nextUpperCase) {
                    sb.append(SEPARATOR);
                }
                upperCase = true;
            } else {
                upperCase = false;
            }

            sb.append(Character.toLowerCase(c));
        }

        return sb.toString();
    }
}

Related

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