Here you can find the source of split(String line, String separator)
Parameter | Description |
---|---|
line | The string line |
separator | The separator |
public static String[] split(String line, String separator)
//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; } } }