关于JAVA处理字符串

来源:百度知道 编辑:UC知道 时间:2024/05/24 11:16:36
如果有一段英文,如:I am a student. My name is AAA. I like to swim, play basketball and play football.
这里面有逗号、有点号,或者还有其他符号(但如果遇到单引号,如my father's 中的father's要单独作为一个单词),请问要怎样才能把每个单词单独分开来?

请高手帮忙!

回答得好再加分!
xxx

//给你写了小例子 自己看看吧~~
//关键是正则表达式的运用

public class StringTestOne {

public static void main(String[] args) {
String tags = "[,\\.\\s;!?]+";//要分割的标记包括逗号,句号,空白符,感叹号,问号...你可以补充完整
String line = "I am a student. My name is AAA. I like ? Yes i am sure ! " +
" to swim, play basketball and play football.My father's football!";
String [] re = line.split(tags);//根据给定的分割标记分割句子~~
for(int i = 0; i < re.length; i ++) {
System.out.println((i + 1) + ": " + re[i]);
}
}

}
///out put:

1: I
2: am
3: a
4: student
5: My
6: name
7: is
8: AAA
9: I
10: like
11: Yes
12: i
13: am
14: sure
15: to
16: swim
17: play
18: basketball
19: and
20: play
21: football
22: My
23: father's
24: football

1 【软件开发】JAVA字符串处理函数列表一览