java实现分词替换
1、使用分词类:StringTokenizer
2、设置分词字符:, !.;
代码
public static void main(String[] args) {
String s = "I like the cat;";
StringTokenizer st = new StringTokenizer(s, ", !.;", true);
// 按逗号/空格/叹号分割
String match = "";
while (st.hasMoreElements()) {
String cur = st.nextElement().toString();
cur = cur.replace("cat", "dog");
match += cur;
}
System.out.println("" + match);
// new MainUICodeConvert();
}
输出:I like the dog;