Here you can find the source of split(final String input, final char split)
Parameter | Description |
---|---|
input | The string to be split |
split | The char to be used to split the input string |
public static String[] split(final String input, final char split)
//package com.java2s; import java.util.ArrayList; public class Main { /**/*from w w w .j a va 2 s . c o m*/ * Similar to the normal String split function. However this function ignores escaped characters (i.e. \[ ). * * @param input The string to be split * @param split The char to be used to split the input string * @return An array of split strings */ public static String[] split(final String input, final char split) { int index = indexOf(input, split); int prevIndex = 0; final ArrayList<String> output = new ArrayList<String>(); if (index == -1) { output.add(input); return output.toArray(new String[1]); } while (index != -1) { output.add(input.substring(prevIndex, index)); prevIndex = index + 1; index = indexOf(input, split, index + 1); } output.add(input.substring(prevIndex, input.length())); return output.toArray(new String[output.size()]); } /** * Similar to the normal String split function. However this function ignores escaped characters (i.e. \[ ). * * @param input The string to be split * @param split The char to be used to split the input string * @param limit The maximum number of times to split the string * @return An array of split strings */ public static String[] split(final String input, final char split, final int limit) { int index = indexOf(input, split); int prevIndex = 0, count = 1; final ArrayList<String> output = new ArrayList<String>(); if (index == -1) { output.add(input); return output.toArray(new String[1]); } while (index != -1 && count != limit) { output.add(input.substring(prevIndex, index)); prevIndex = index + 1; index = indexOf(input, split, index + 1); count++; } output.add(input.substring(prevIndex, input.length())); return output.toArray(new String[output.size()]); } /** * Gets the first index of a character ignoring characters that have been escaped * * @param input The string to be searched * @param delim The character to be found * @return The index of the found character or -1 if the character wasn't found */ public static int indexOf(final String input, final char delim) { return indexOf(input, delim, 0); } /** * Gets the first index of a character after fromIndex. Ignoring characters that have been escaped * * @param input The string to be searched * @param delim The character to be found * @param fromIndex Start searching from this index * @return The index of the found character or -1 if the character wasn't found */ public static int indexOf(final String input, final char delim, final int fromIndex) { if (input == null) return -1; int index = input.indexOf(delim, fromIndex); if (index != 0) { while (index != -1 && index != (input.length() - 1)) { if (input.charAt(index - 1) != '\\') break; index = input.indexOf(delim, index + 1); } } return index; } }