Java String Lower Case toLowerCaseWithSepStyle(String string, String separator)

Here you can find the source of toLowerCaseWithSepStyle(String string, String separator)

Description

Converts compound names to the "lower case with separtor style".

License

Open Source License

Parameter

Parameter Description
string the input string.
separator the separator

Declaration


public static String toLowerCaseWithSepStyle(String string, String separator) 

Method Source Code

//package com.java2s;
/*//w  ww.  java2 s.  c  o  m
 * The MIT License (MIT)
 * 
 * Copyright (c) 2010 Technische Universitaet Berlin
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

public class Main {
    /**
     * <p>
     *   Converts compound names  to the "lower case with separtor style". That meas that all
     *   components of the name are converted to lower case and concatenated with a certain
     *   string, the separator. The input string is expected to be in the "upcase first
     *   character style", See {@link #toUpcaseFirstStyle toUpcaseFirstStyle} for an
     *   explanation of that term.
     * </p>
     * <p>
     *   Example 1:<pre>
     *     toLowerCaseWithSepStyle("FooBarBazz", "-")</pre>
     *   returns <code>"foo-bar-bazz"</code>.
     * </p>
     * <p>
     *   Example 1:<pre>
     *     toLowerCaseWithSepStyle("XSLStylesheet", "_")</pre>
     *   returns <code>"xsl_stylesheet"</code>.
     * </p>
     * @param string the input string.
     * @param separator the separator
     */

    public static String toLowerCaseWithSepStyle(String string, String separator) {
        char[] chars = string.toCharArray();
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < chars.length; i++) {
            if (Character.isUpperCase(chars[i]) && // char is upper case
                    i > 0 && // 
                    (Character.isLowerCase(chars[i - 1])
                            || (i < chars.length - 1 && Character.isLowerCase(chars[i + 1]))))
                buffer.append(separator);
            buffer.append(Character.toLowerCase(chars[i]));
        }
        return buffer.toString();
    }
}

Related

  1. toLowerCaseInitial(String srcString, boolean flag)
  2. toLowerCaseSafe(String str)
  3. toLowercaseSlug(String string)
  4. toLowerCaseSubsystemType(String s)
  5. toLowercaseWithDashes(String text)
  6. toLowerCaseWithUnderscores(String className)
  7. toLowerCaseWithUnderscores(String str)