Java String Split split(String s, int c)

Here you can find the source of split(String s, int c)

Description

Split a string using the char code of a character.

License

Open Source License

Parameter

Parameter Description
s the string to split
c the integer code of the character

Return

an array containing the split string (or empty if the string is null)

Declaration

public final static String[] split(String s, int c) 

Method Source Code

//package com.java2s;
/*******************************************************************************
  * GenPlay, Einstein Genome Analyzer/*from  www  . j av  a 2  s.  co m*/
  * Copyright (C) 2009, 2014 Albert Einstein College of Medicine
  * 
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  * 
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  * 
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  * Authors: Julien Lajugie <julien.lajugie@einstein.yu.edu>
  *          Nicolas Fourel <nicolas.fourel@einstein.yu.edu>
  *          Eric Bouhassira <eric.bouhassira@einstein.yu.edu>
  * 
  * Website: <http://genplay.einstein.yu.edu>
  ******************************************************************************/

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
      * Split a string using the char code of a character.
      * @param s   the string to split
      * @param c   the integer code of the character
      * @return   an array containing the split string (or empty if the string is null)
      */
    public final static String[] split(String s, int c) {
        List<String> list = new ArrayList<String>();
        if (s != null) {
            int pos = 0, end;
            while ((end = s.indexOf(c, pos)) >= 0) {
                String sub = s.substring(pos, end);
                list.add(sub);
                pos = end + 1;
            }
            list.add(s.substring(pos));
        }
        if (list.isEmpty()) {
            return null;
        }
        int size = list.size();
        if (list.get(size - 1).isEmpty()) {
            size--;
        }

        String[] result = new String[size];
        for (int i = 0; i < size; i++) {
            result[i] = list.get(i);
        }

        return result;
    }
}

Related

  1. split(String s)
  2. split(String s)
  3. split(String s)
  4. split(String s)
  5. split(String s)
  6. split(String s, String at)
  7. split(String s, String s1)
  8. split(String s, String seperator)
  9. split(String s, String spliter, boolean removeEmptyItem)