单词分割

Description
从键盘输入一段英文,将其中的英文单词分离出来:已知单词之间的分隔符包括空格,问号、句号(小数点)和分号。 
Input
输入一行字符串 (字符不超过1000)
Output
将分割后的单词按行输出
Sample Input
There are apples; oranges and peaches on the table.
Sample Output
There
are
apples
oranges
and
peaches
on
the

table

import java.util.Scanner;

代码
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
String str = cin.nextLine();
String[] arr = str.replaceAll("[^a-zA-Z]+", " ").split("[^a-zA-Z]");
for(int i=0;i<arr.length;i++) {
System.out.println(arr[i]);
}

}
}

猜你喜欢

转载自blog.csdn.net/progammer10086/article/details/81005905