Java String Split by Char split(String string, char character)

Here you can find the source of split(String string, char character)

Description

split

License

Open Source License

Declaration

public static final String[] split(String string, char character) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2005, 2007 Remy Suen//  ww w. j  av a2 s .  co m
 * 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:
 *    Remy Suen <remy.suen@gmail.com> - initial API and implementation
 ******************************************************************************/

import java.util.ArrayList;

public class Main {
    public static final String[] split(String string, char character) {
        int index = string.indexOf(character);
        if (index == -1) {
            return new String[] { string };
        }

        ArrayList split = new ArrayList();
        while (index != -1) {
            split.add(string.substring(0, index));
            string = string.substring(index + 1);
            index = string.indexOf(character);
        }

        if (!string.equals("")) { //$NON-NLS-1$
            split.add(string);
        }

        return (String[]) split.toArray(new String[split.size()]);
    }

    public static final String[] split(String string, String delimiter) {
        int index = string.indexOf(delimiter);
        if (index == -1) {
            return new String[] { string };
        }

        int length = delimiter.length();
        ArrayList split = new ArrayList();
        while (index != -1) {
            split.add(string.substring(0, index));
            string = string.substring(index + length);
            index = string.indexOf(delimiter);
        }

        if (!string.equals("")) { //$NON-NLS-1$
            split.add(string);
        }

        return (String[]) split.toArray(new String[split.size()]);
    }

    public static final String[] split(String string, String delimiter,
            int limit) {
        int index = string.indexOf(delimiter);
        if (index == -1) {
            return new String[] { string };
        }

        int count = 0;
        int length = delimiter.length();
        ArrayList split = new ArrayList(limit);
        while (index != -1 && count < limit - 1) {
            split.add(string.substring(0, index));
            string = string.substring(index + length);
            index = string.indexOf(delimiter);
            count++;
        }

        if (!string.equals("")) { //$NON-NLS-1$
            split.add(string);
        }

        return (String[]) split.toArray(new String[split.size()]);
    }
}

Related

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