本文发表在 rolia.net 枫下论坛import java.util.*;
public class TestTokenization {
private static List tokenize(String str, String dms) {
ArrayList list = new ArrayList( );
StringTokenizer tok = new StringTokenizer(str, dms);
while (tok.hasMoreTokens()) {
list.add(tok.nextToken());
}
return list;
}
private static void refresh(List list, int position, String s) {
if (position >= list.size() ) {
return;
}
String temp = (String) list.get(position);
if (temp.endsWith(s)) {
temp = temp.substring(0, temp.indexOf(s))
+ (String) list.get(position + 1);
list.set(position, temp);
list.remove(position + 1);
}
refresh(list, ++position, s);
}
private static void prt(List list) {
Iterator it = list.iterator();
while (it.hasNext()) {
System.out.print(it.next() + " ");
}
}
public static void main(String[] args) {
String s = "abc\\|ef|gh|lmn\\|\\zx|yw";
List list = tokenize(s, "|");
prt(list);
System.out.println("");
refresh(list, 0, "\\");
prt(list);
}
}更多精彩文章及讨论,请光临枫下论坛 rolia.net
public class TestTokenization {
private static List tokenize(String str, String dms) {
ArrayList list = new ArrayList( );
StringTokenizer tok = new StringTokenizer(str, dms);
while (tok.hasMoreTokens()) {
list.add(tok.nextToken());
}
return list;
}
private static void refresh(List list, int position, String s) {
if (position >= list.size() ) {
return;
}
String temp = (String) list.get(position);
if (temp.endsWith(s)) {
temp = temp.substring(0, temp.indexOf(s))
+ (String) list.get(position + 1);
list.set(position, temp);
list.remove(position + 1);
}
refresh(list, ++position, s);
}
private static void prt(List list) {
Iterator it = list.iterator();
while (it.hasNext()) {
System.out.print(it.next() + " ");
}
}
public static void main(String[] args) {
String s = "abc\\|ef|gh|lmn\\|\\zx|yw";
List list = tokenize(s, "|");
prt(list);
System.out.println("");
refresh(list, 0, "\\");
prt(list);
}
}更多精彩文章及讨论,请光临枫下论坛 rolia.net