Here you can find the source of SplitAt(String str, String delimiter)
Parameter | Description |
---|---|
str | A string to split. |
delimiter | A string to signal where each substring begins and ends. |
Parameter | Description |
---|---|
IllegalArgumentException | Delimiter is null or empty. |
NullPointerException | The parameter delimiter is null. |
public static String[] SplitAt(String str, String delimiter)
//package com.java2s; import java.util.*; public class Main { /**/* ww w . j ava2 s. co m*/ * Splits a string by a delimiter. If the string ends with the delimiter, the * result will end with an empty string. If the string begins with the * delimiter, the result will start with an empty string. * @param str A string to split. * @param delimiter A string to signal where each substring begins and ends. * @return An array containing strings that are split by the delimiter. If str * is null or empty, returns an array whose sole element is the empty * string. * @throws IllegalArgumentException Delimiter is null or empty. * @throws NullPointerException The parameter {@code delimiter} is null. */ public static String[] SplitAt(String str, String delimiter) { if (delimiter == null) { throw new NullPointerException("delimiter"); } if (delimiter.length() == 0) { throw new IllegalArgumentException("delimiter is empty."); } if (((str) == null || (str).length() == 0)) { return new String[] { "" }; } int index = 0; boolean first = true; ArrayList<String> strings = null; int delimLength = delimiter.length(); while (true) { int index2 = str.indexOf(delimiter, index); if (index2 < 0) { if (first) { String[] strret = new String[1]; strret[0] = str; return strret; } strings = (strings == null) ? ((new ArrayList<String>())) : strings; strings.add(str.substring(index)); break; } else { first = false; String newstr = str.substring(index, (index) + ((index2) - index)); strings = (strings == null) ? ((new ArrayList<String>())) : strings; strings.add(newstr); index = index2 + delimLength; } } return strings.toArray(new String[] {}); } }