Split a string into pieces along a specific character. Most commonly used to break up a String along a space or a tab character. This operates differently than the others, where the single delimeter is the only breaking point, and consecutive delimeters will produce an empty string (""). This wa
(String what, char delim)
| 103 | * column alignments (of say an excel file) where there are empty columns. |
| 104 | */ |
| 105 | static public String[] split(String what, char delim) { |
| 106 | // do this so that the exception occurs inside the user's |
| 107 | // program, rather than appearing to be a bug inside split() |
| 108 | if (what == null) |
| 109 | return null; |
| 110 | |
| 111 | char chars[] = what.toCharArray(); |
| 112 | int splitCount = 0; // 1; |
| 113 | for (int i = 0; i < chars.length; i++) { |
| 114 | if (chars[i] == delim) |
| 115 | splitCount++; |
| 116 | } |
| 117 | if (splitCount == 0) { |
| 118 | String splits[] = new String[1]; |
| 119 | splits[0] = new String(what); |
| 120 | return splits; |
| 121 | } |
| 122 | String splits[] = new String[splitCount + 1]; |
| 123 | int splitIndex = 0; |
| 124 | int startIndex = 0; |
| 125 | for (int i = 0; i < chars.length; i++) { |
| 126 | if (chars[i] == delim) { |
| 127 | splits[splitIndex++] = new String(chars, startIndex, i - startIndex); |
| 128 | startIndex = i + 1; |
| 129 | } |
| 130 | } |
| 131 | splits[splitIndex] = new String(chars, startIndex, chars.length |
| 132 | - startIndex); |
| 133 | return splits; |
| 134 | } |
| 135 | |
| 136 | static public String[] subset(String list[], int start, int count) { |
| 137 | String output[] = new String[count]; |
no outgoing calls