java - I am not getting why split() and StringTokenizer() behaving differently -
public class newclass { public static void main(string args[]) { system.out.println("operation 1"); stringtokenizer st1 = new stringtokenizer("hello geeks how you", "\\s+"); while (st1.hasmoretokens()) system.out.println(st1.nexttoken()); string s = "hello geeks how you"; string s1[]= s.split("\\s+"); system.out.println("operation 2"); for( string temp : s1) { system.out.println(temp); } } } here after executing code getting output :-
operation 1 hello geek how operation 2 hello geeks how i not getting why split() , stringtokenizer() behaving differently same parameter.
the delim parameter of new stringtokenizer(str, delim) not regular expression. telling stringtokenizer split @ any of \, s, or +, not @ "one or more whitespace characters". , character applies in string "s" in "geeks", that's string split.
if want split @ whitespaces (one or more), use other constructor taking no delim parameter, using " \t\n\r\f" default.
Comments
Post a Comment