Java String Underscore underscore(final String s, final Locale locale)

Here you can find the source of underscore(final String s, final Locale locale)

Description

Converts the given string to 'underscore' style.

License

Apache License

Parameter

Parameter Description
s the camel case string
locale the Locale to use when converting to lowercase

Return

the underscored string

Declaration

public static String underscore(final String s, final Locale locale) 

Method Source Code

//package com.java2s;
/**/*from   w w w  .j  ava2s .co  m*/
 * Copyright (c) 2008-2009 Acuity Technologies, Inc.
 *
 * 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.
 *
 * Created Jan 1, 2008
 */

import java.util.ArrayList;
import java.util.Collection;

import java.util.Iterator;
import java.util.Locale;

public class Main {
    /**
     * Converts the given string to 'underscore' style. First, the string is
     * split into individual camel case words. Each word is then converted to
     * all lower case, then all the words are joined together by underscores.
     * Converts "camelCase" to "camel_case".
     *
     * @param s
     *        the camel case string
     * @return the underscored string
     */
    public static String underscore(final String s) {
        return underscore(s, Locale.getDefault());
    }

    /**
     * Converts the given string to 'underscore' style. First, the string is
     * split into individual camel case words. Each word is then converted to
     * all lower case, then all the words are joined together by underscores.
     * Converts "camelCase" to "camel_case".
     *
     * @param s
     *        the camel case string
     * @param locale
     *        the Locale to use when converting to lowercase
     * @return the underscored string
     */
    public static String underscore(final String s, final Locale locale) {
        final String[] words = splitCamelCase(s);
        for (int i = 0; i < words.length; ++i) {
            words[i] = words[i].toLowerCase(locale);
        }
        return join(words, '_');
    }

    /**
     * Splits a camel case string into individual words.
     *
     * @param s
     *        the camel case string
     * @return the individual words as a string array
     */
    public static String[] splitCamelCase(final String s) {
        final ArrayList<String> words = new ArrayList<String>();
        final int length = s.length();
        int i = 0;
        final StringBuilder sb = new StringBuilder();
        while (i < length) {
            while (i < length && Character.isUpperCase(s.charAt(i))) {
                sb.append(s.charAt(i));
                ++i;
            }
            while (i < length && !Character.isUpperCase(s.charAt(i))) {
                sb.append(s.charAt(i));
                ++i;
            }
            final String word = sb.toString();
            words.add(word);
            sb.setLength(0);
        }
        return words.toArray(new String[words.size()]);
    }

    /**
     * Join a variable argument list using commas.
     *
     * @param args
     *        the objects
     * @return The joined String
     */
    public static String join(final Object... args) {
        return join(",", args);
    }

    /**
     * Join a variable argument list of objects with the given delimiter
     * character.
     *
     * @param args
     *        the objects
     * @param c
     *        the delimiter character
     * @return The joined String
     */
    public static String join(final char c, final Object... args) {
        return join(String.valueOf(c), args);
    }

    /**
     * Join a variable argument list of objects with the given delimiter String.
     *
     * @param args
     *        the objects
     * @param delimiter
     *        the delimiter String
     * @return The joined String
     */
    public static String join(final String delimiter, final Object... args) {
        final StringBuilder sb = new StringBuilder();
        for (int i = 0; i < args.length; ++i) {
            sb.append(args[i]);
            if (i + 1 < args.length) {
                sb.append(delimiter);
            }
        }
        return sb.toString();
    }

    /**
     * Join an array of objects with the given delimiter character.
     *
     * @param <T>
     *        a type
     * @param array
     *        the String array
     * @param c
     *        the delimiter character
     * @return The joined String
     */
    public static <T> String join(final T[] array, final char c) {
        return join(array, String.valueOf(c));
    }

    /**
     * Join an array of objects with the given delimiter String.
     *
     * @param <T>
     *        a type
     * @param array
     *        the String array
     * @param delimiter
     *        the delimiter String
     * @return The joined String
     */
    public static <T> String join(final T[] array, final String delimiter) {
        final StringBuilder sb = new StringBuilder();
        for (int i = 0; i < array.length; ++i) {
            sb.append(array[i]);
            if (i + 1 < array.length) {
                sb.append(delimiter);
            }
        }
        return sb.toString();
    }

    /**
     * Joins a {@link Collection} of arbitrary objects with the given delimiter
     * character.
     *
     * @param <T>
     *        a type
     * @param collection
     *        the collection to join
     * @param c
     *        the delimiter character
     * @return The joined String
     */
    public static <T> String join(final Collection<T> collection, final char c) {
        return join(collection, String.valueOf(c));
    }

    /**
     * Joins a {@link Collection} of arbitrary objects with the given delimiter
     * String.
     *
     * @param <T>
     *        a type
     * @param collection
     *        the collection to join
     * @param delimiter
     *        the delimiter string
     * @return The joined String
     */
    public static <T> String join(final Collection<T> collection, final String delimiter) {
        final StringBuilder sb = new StringBuilder();
        for (final Iterator<T> i = collection.iterator(); i.hasNext();) {
            sb.append(i.next());
            if (i.hasNext()) {
                sb.append(delimiter);
            }
        }
        return sb.toString();
    }
}

Related

  1. toUnderscored(String name)
  2. toUnderscoredLowercase(String text)
  3. toUnderscoreName(String name)
  4. toUnderscores(String s)
  5. toUnderscoreSeparated(String name)
  6. underscore(String camel)
  7. underscore(String camelCaseWord)
  8. underscore(String field)
  9. underscore(String phase)