Java String Split by Separator split(String str, char separator)

Here you can find the source of split(String str, char separator)

Description

we can't use String.split as it is a JDK 1.4 method.

License

Apache License

Parameter

Parameter Description
str a parameter
separator a parameter

Return

an array of string segments

Declaration

static public String[] split(String str, char separator) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004, 2006 svnClientAdapter project and others.
 *
 * 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.//from  w  ww.  j ava 2s  .c  om
 *
 * Contributors:
 *     svnClientAdapter project committers - initial API and implementation
 ******************************************************************************/

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * we can't use String.split as it is a JDK 1.4 method.
     * //TODO Java 1.4 is not aproblem anymore. Isn't it ? 
     * @param str
     * @param separator
     * @return an array of string segments
     */
    static public String[] split(String str, char separator) {
        int pos = 0;
        List list = new ArrayList();
        int length = str.length();
        for (int i = 0; i < length; i++) {
            char ch = str.charAt(i);
            if (ch == separator) {
                list.add(str.substring(pos, i));
                pos = i + 1;
            }
        }
        if (pos != length) {
            list.add(str.substring(pos, length));
        }
        return (String[]) list.toArray(new String[list.size()]);
    }

    /**
     * split using a string separator
     * @param str
     * @param separator
     * @return an array of string segments
     */
    static public String[] split(String str, String separator) {
        List list = new ArrayList();
        StringBuffer sb = new StringBuffer(str);
        int pos;

        while ((pos = sb.indexOf(separator)) != -1) {
            list.add(sb.substring(0, pos));
            sb.delete(0, pos + separator.length());
        }
        if (sb.length() > 0) {
            list.add(sb.toString());
        }
        return (String[]) list.toArray(new String[list.size()]);
    }
}

Related

  1. split(String s, String separator)
  2. split(String src, char separator)
  3. split(String src, char separator, boolean trim)
  4. split(String str, char separator)
  5. split(String str, char separator)
  6. split(String str, char separator)
  7. split(String str, char separatorChar)
  8. split(String str, char separatorChar)
  9. split(String str, char separatorChar)