这是输入
hello; this is cool?
great, awesome
我希望我的输出
hello;
this
is
cool?
great,
awesome
我基本上考虑用一个词来标注符号。这是我对我的应用程序的定义。我想基于空格,制表空间和换行符分割单词。大多数stackoverflow问题和答案都假设单词不包含标点符号,那么我该如何解决呢?
答案 0 :(得分:-1)
直接在代码中注释和解释:
//1st possibility: every single whitespace character (space, tab, newline, carriage return, vertical tab) will be treated as a separator
String s="hello; this is cool?\ngreat, awesome";
String[] array1 = s.split("\\s");
System.out.println("======first case=====");
for(int i=0; i<array1.length; i++)
System.out.println(array1[i]);
//2nd possibility (groups of consecutive whitespace characters (space, tab, newline, carriage return, vertical tab) will be treated as a single separator
String[] array2 = s.split("\\s+");
System.out.println("=====second case=====");
for(int i=0; i<array2.length; i++)
System.out.println(array2[i]);
//notice the difference in the output!!!
<强>输出:强>
======first case=====
hello;
this
is
cool?
great,
<----- Notice the empty string
<----- Notice the empty string
awesome
=====second case=====
hello;
this
is
cool?
great,
awesome