Java String Split by Char split(String value, char splitChar)

Here you can find the source of split(String value, char splitChar)

Description

split strings by the character.

License

Open Source License

Parameter

Parameter Description
value a parameter
splitChar a parameter

Declaration

public static String[] split(String value, char splitChar) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004,2009 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/* ww  w  .j av  a 2  s .  co m*/
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

import java.util.ArrayList;

public class Main {
    /**
     * split strings by the character.
     * 
     * It implements javascript's behavior as always return count(splitChar)+1
     * 
     * for example, split char is '/':
     * 
     * <dl>
     * <li>'/' => ['', '']</li>
     * <li>'/abc/' => ['', 'abc', '']</li>
     * <li>'abc' => 'abc'</li>
     * </dl>
     * 
     * @param value
     * @param splitChar
     * @return
     */
    public static String[] split(String value, char splitChar) {
        ArrayList<String> result = new ArrayList<String>();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < value.length(); i++) {
            char ch = value.charAt(i);
            if (ch == splitChar) {
                result.add(sb.toString());
                sb.setLength(0);
            } else {
                sb.append(ch);
            }
        }
        result.add(sb.toString());
        return result.toArray(new String[result.size()]);
    }
}

Related

  1. Split(String Str, char splitchar)
  2. split(String string, char c)
  3. split(String string, char character)
  4. split(String toSplit, char splitChar, boolean trim)
  5. split(String val, char ch)
  6. splitAt(String inputString, Character inputChar)
  7. splitAt(String str, char c)
  8. splitByChar(final String message, final char ch)
  9. splitByNonNestedChars(String s, char... c)