/**
* Splits a string in several parts (tokens) that are separated by delimiter
* characters. Delimiter may contains any number of character and it is
* always surrounded by two strings.
*
* @param src source to examine
* @param d string with delimiter characters
*
* @return array of tokens
*/
public static String[] splitc(final String src, final String d) {
if ((d.length() == 0) || (src.length() == 0)) {
return new String[] {src};
}
return splitc(src, d.toCharArray());
}
/**
* Splits a string in several parts (tokens) that are separated by delimiter
* characters. Delimiter may contains any number of character and it is
* always surrounded by two strings.
*
* @param src source to examine
* @param delimiters char array with delimiter characters
*
* @return array of tokens
*/
public static String[] splitc(final String src, final char[] delimiters) {
if ((delimiters.length == 0) || (src.length() == 0) ) {
return new String[] {src};
}
char[] srcc = src.toCharArray();
int maxparts = srcc.length + 1;
int[] start = new int[maxparts];
int[] end = new int[maxparts];
int count = 0;
start[0] = 0;
int s = 0, e;
if (CharUtil.equalsOne(srcc[0], delimiters)) { // string starts with delimiter
end[0] = 0;
count++;
s = CharUtil.findFirstDiff(srcc, 1, delimiters);
if (s == -1) { // nothing after delimiters
return new String[] {EMPTY, EMPTY};
}
start[1] = s; // new start
}
while (true) {
// find new end
e = CharUtil.findFirstEqual(srcc, s, delimiters);
if (e == -1) {
end[count] = srcc.length;
break;
}
end[count] = e;
// find new start
count++;
s = CharUtil.findFirstDiff(srcc, e, delimiters);
if (s == -1) {
start[count] = end[count] = srcc.length;
break;
}
start[count] = s;
}
count++;
String[] result = new String[count];
for (int i = 0; i < count; i++) {
result[i] = src.substring(start[i], end[i]);
}
return result;
}
/**
* Splits a string in several parts (tokens) that are separated by single delimiter
* characters. Delimiter is always surrounded by two strings.
*
* @param src source to examine
* @param delimiter delimiter character
*
* @return array of tokens
*/
public static String[] splitc(final String src, final char delimiter) {
if (src.length() == 0) {
return new String[] {EMPTY};
}
char[] srcc = src.toCharArray();
int maxparts = srcc.length + 1;
int[] start = new int[maxparts];
int[] end = new int[maxparts];
int count = 0;
start[0] = 0;
int s = 0, e;
if (srcc[0] == delimiter) { // string starts with delimiter
end[0] = 0;
count++;
s = CharUtil.findFirstDiff(srcc, 1, delimiter);
if (s == -1) { // nothing after delimiters
return new String[] {EMPTY, EMPTY};
}
start[1] = s; // new start
}
while (true) {
// find new end
e = CharUtil.findFirstEqual(srcc, s, delimiter);
if (e == -1) {
end[count] = srcc.length;
break;
}
end[count] = e;
// find new start
count++;
s = CharUtil.findFirstDiff(srcc, e, delimiter);
if (s == -1) {
start[count] = end[count] = srcc.length;
break;
}
start[count] = s;
}
count++;
String[] result = new String[count];
for (int i = 0; i < count; i++) {
result[i] = src.substring(start[i], end[i]);
}
return result;
}
Splits a string in several parts (tokens)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- Day 12 神句文档 The team contends that these bear more than a...
- Problem More LeetCode DiscussionsGiven an input string, r...