Java String Split by Char split(String s, char divider)

Here you can find the source of split(String s, char divider)

Description

Split a string into pieces based on the given divider character.

License

Open Source License

Parameter

Parameter Description
s the string to split
divider the character on which to split. Occurrences of this character are not included in the output

Return

An ArrayList of the pieces.

Declaration

public static ArrayList split(String s, char divider) 

Method Source Code

//package com.java2s;
/**//from ww  w  . j ava2s . c  om
 * Util.java
 * @author John Green
 * 27-Oct-2002
 * www.joanju.com
 * 
 * Copyright (c) 2002 Joanju Limited.
 * 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
 * 
 */

import java.util.*;

public class Main {
    /**   
     * Split a string into pieces based on the given divider character.
     * Used for Java 1.3 compatability - String.split() is new as of 1.4.
     * @deprecated Use Java 1.4 String.split() instead.   
     * @param s the string to split   
     * @param divider the character on which to split.  Occurrences of   
     * this character are not included in the output   
     * @return An ArrayList of the pieces.
     */
    public static ArrayList split(String s, char divider) {
        ArrayList retList = new ArrayList();
        int last = 0;
        int sLength = s.length();
        int i = 0;
        for (; i < sLength; ++i) {
            if (s.charAt(i) == divider) {
                retList.add(s.substring(last, i));
                last = i + 1;
            }
        }
        retList.add(s.substring(last, i));
        return retList;
    }
}

Related

  1. split(String s, char c)
  2. split(String s, char c)
  3. split(String s, char c, int limit)
  4. split(String s, char c, int limit)
  5. split(String s, char ch)
  6. split(String s, char divider, String[] output)
  7. split(String s, char sep)
  8. split(String s, char sep)
  9. split(String splittee, String splitChar)