我想知道我们如何分隔句子的单词,其中分隔符是''(空格)或'?' 要么 '。'。 对于前
输入:THIS IS A STRING PROGRAM.IS THIS EASY?YES,IT IS
。
输出:
THIS
IS
A
STRING
PROGRAM
IS
THIS
EASY
YES
IT
IS
答案 0 :(得分:1)
请参阅Java中StringTokenizer类的构造函数。它可以接受自定义分隔符。
试试这个:
StringTokenizer tokenizer = new StringTokenizer("THIS IS A STRING PROGRAM.IS THIS EASY?YES,IT IS", " .?");
while (tokenizer.hasMoreElements()) {
System.out.println(tokenizer.nextElement());
}
答案 1 :(得分:0)
public static void main(String[] args) {
String str = "THIS IS A STRING PROGRAM.IS THIS EASY?YES,IT IS";
StringTokenizer st = new StringTokenizer(str);
System.out.println("---- Split by space ------");
while (st.hasMoreElements()) {
System.out.println(st.nextElement());
}
System.out.println("---- Split by comma ',' ------");
StringTokenizer st2 = new StringTokenizer(str, ",");
while (st2.hasMoreElements()) {
System.out.println(st2.nextElement());
}
}