Java String Split by Case splitCamelCase(final String s)

Here you can find the source of splitCamelCase(final String s)

Description

Splits a camel case string into individual words.

License

Apache License

Parameter

Parameter Description
s the camel case string

Return

the individual words as a string array

Declaration

public static String[] splitCamelCase(final String s) 

Method Source Code

//package com.java2s;
/**/*  w ww  .  j  a v a 2 s.com*/
 * 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;

public class Main {
    /**
     * 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()]);
    }
}

Related

  1. split(String pString, String pSeparator, boolean pIgnoreCase)
  2. splitByCamelCase(String name)
  3. splitByCharacterTypeCamelCase(String targetName)
  4. splitCamelCase(final String in)
  5. splitCamelCase(String src)
  6. splitCamelCase(String string)
  7. splitOnUppercase(String input)
  8. splitString(String string, Character character, boolean encased)