Here you can find the source of split(final String str)
Parameter | Description |
---|---|
str | the source string. |
public static String[] split(final String str)
//package com.java2s; /***************************************************************** Copyright 2006 by Dung Nguyen (dungnguyen@truthinet.com) //from w w w. j a v a 2 s.c om Licensed under the iNet Solutions Corp.,; you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.truthinet.com/licenses Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *****************************************************************/ import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Main { /** the empty string. */ public static final String EMPTY_STRING = ""; /** comma character. */ public static final char COMMA = ','; /** escape character. */ public static final char ESCAPE = '\\'; /** * Split a given string. * * @param str the source string. * @return the array of none empty string. */ public static String[] split(final String str) { return split(str, ESCAPE, COMMA); } /** * Split a given string using the given separator. * * @param str the given string to split. * @param escapeChar the given escape character. * @param separator the given separator. * @return the array of none empty string. */ public static String[] split(final String str, char escapeChar, char separator) { // null string. if (str == null) { return null; } // create storage to store data. final List<String> strings = new ArrayList<String>(); // split data. final StringBuilder split = new StringBuilder(); int index = 0; while ((index = findNext(str, separator, escapeChar, index, split)) >= 0) { ++index; strings.add(split.toString()); split.setLength(0); // reset buffer. } // add the last string. strings.add(split.toString()); // remove empty character. final Iterator<String> iterator = strings.iterator(); while (iterator.hasNext()) { if (EMPTY_STRING.equals(iterator.next())) { iterator.remove(); } } // return split data. return strings.toArray(new String[strings.size()]); } /** * Finds the first occurrence of the separator character ignoring the escaped separators * starting from the index. * * @param str the source string. * @param separator the separator character. * @param escapeChar the escape character. * @param start from where to search. * @param split used to pass back the extracted string. * @return the first occurrence separator; or -1 if the separator could not be found. */ public static int findNext(final String str, char separator, char escapeChar, int start, final StringBuilder split) { int numPreEscapes = 0; for (int index = start; index < str.length(); index++) { char ch = str.charAt(index); if (numPreEscapes == 0 && ch == separator) { // separator. return index; } else { split.append(ch); numPreEscapes = (ch == escapeChar) ? (++numPreEscapes) % 2 : 0; } } return -1; } }