Here you can find the source of toUnderscoreCase(final String s)
Parameter | Description |
---|---|
s | The text to convert. |
public static String toUnderscoreCase(final String s)
//package com.java2s; /*// w ww . ja v a2 s .c o m * Copyright 2012 Daniel Bechler * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ public class Main { /** * Converts an optionally camel-cased character sequence (e.g. ThisIsSparta) into underscore-case (e.g. * this_is_sparta). * * @param s The text to convert. * * @return A underscore-cased version of the given text. */ public static String toUnderscoreCase(final String s) { 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(); } }