The split() function breaks a String into pieces using a character or string as the delimiter. The delim parameter specifies the character or characters that mark the boundaries between each piece. A String[] array is returned that contains each of the pieces. If the resu
(String value, char delim)
| 8418 | * the character or String used to separate the data |
| 8419 | */ |
| 8420 | static public String[] split(String value, char delim) { |
| 8421 | // do this so that the exception occurs inside the user's |
| 8422 | // program, rather than appearing to be a bug inside split() |
| 8423 | if (value == null) return null; |
| 8424 | //return split(what, String.valueOf(delim)); // huh |
| 8425 | |
| 8426 | char[] chars = value.toCharArray(); |
| 8427 | int splitCount = 0; //1; |
| 8428 | for (char ch : chars) { |
| 8429 | if (ch == delim) splitCount++; |
| 8430 | } |
| 8431 | // make sure that there is something in the input string |
| 8432 | //if (chars.length > 0) { |
| 8433 | // if the last char is a delimiter, get rid of it.. |
| 8434 | //if (chars[chars.length-1] == delim) splitCount--; |
| 8435 | // on second thought, i don't agree with this, will disable |
| 8436 | //} |
| 8437 | if (splitCount == 0) { |
| 8438 | String[] splits = new String[1]; |
| 8439 | splits[0] = value; |
| 8440 | return splits; |
| 8441 | } |
| 8442 | //int pieceCount = splitCount + 1; |
| 8443 | String[] splits = new String[splitCount + 1]; |
| 8444 | int splitIndex = 0; |
| 8445 | int startIndex = 0; |
| 8446 | for (int i = 0; i < chars.length; i++) { |
| 8447 | if (chars[i] == delim) { |
| 8448 | splits[splitIndex++] = |
| 8449 | new String(chars, startIndex, i-startIndex); |
| 8450 | startIndex = i + 1; |
| 8451 | } |
| 8452 | } |
| 8453 | //if (startIndex != chars.length) { |
| 8454 | splits[splitIndex] = |
| 8455 | new String(chars, startIndex, chars.length-startIndex); |
| 8456 | //} |
| 8457 | return splits; |
| 8458 | } |
| 8459 | |
| 8460 | |
| 8461 | static public String[] split(String value, String delim) { |
no test coverage detected