public class NewClass
{
public static void main(String args[])
{
System.out.println("Operation 1");
StringTokenizer st1 =
new StringTokenizer("Hello Geeks How are you", "\\s+");
while (st1.hasMoreTokens())
System.out.println(st1.nextToken());
String s = "Hello Geeks How are you";
String s1[]= s.split("\\s+");
System.out.println("Operation 2");
for( String temp : s1) {
System.out.println(temp);
}
}
}
Run Code Online (Sandbox Code Playgroud)
执行代码后,我得到的输出为: -
Operation 1
Hello Geek
How are you
Operation 2
Hello
Geeks
How
are
you
Run Code Online (Sandbox Code Playgroud)
我不明白为什么split()和StringTokenizer()对相同的参数表现不同.