Java String Split by Line split(String line, String separator)

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

Description

Split a string line by separator

License

Open Source License

Parameter

Parameter Description
line The string line
separator The separator

Return

Splitted string array

Declaration

public static String[] split(String line, String separator) 

Method Source Code

//package com.java2s;
/* Copyright 2012 Yaqiang Wang,
* yaqiang.wang@gmail.com//w w  w. ja  v  a 2 s  .c om
* 
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or (at
* your option) any later version.
* 
* This library 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 Lesser
* General Public License for more details.
*/

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Split a string line by separator
     *
     * @param line The string line
     * @param separator The separator
     * @return Splitted string array
     */
    public static String[] split(String line, String separator) {
        if (separator == null || separator.equals(" ")) {
            return line.split("\\s+");
        } else {
            //String[] strs = line.split(separator + "|\\s+");
            String[] strs = line.split(separator);
            List<String> r = new ArrayList<>();
            for (String s : strs) {
                if (!s.isEmpty())
                    r.add(s);
            }
            strs = r.toArray(new String[1]);
            return strs;
        }
    }
}

Related

  1. split(String line, String delimiter)
  2. split(String line, String regex)
  3. split(String line, String separator)
  4. split(String line, String separator)
  5. split(String line, String separator)
  6. split(String line, String seperator)
  7. Split(String line, String sptr)
  8. splitArguments(String commandLine, boolean backslashesAreLiteral)
  9. splitArgumentsBackslashesAreLiteral(String commandLine)