We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
it produces unwanted result if there are leading or trainling
white spaces so you will need to use s.trim() method before
performing any string operation
Try following code.
publicclassTestWhiteSpace{publicstaticvoidmain(String[]args){Stringtest_str=" a b ";String[]test=test.split("[ ]+");System.out.println(test.length());intcount=0;for(Stringstr:test){count++;Syste.out.println(str+": "+count);}}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java String Tokens
You are viewing a single comment's thread. Return to all comments →
if the input string consists of only white spaces
the
s.split(" ")
will give 0it produces unwanted result if there are leading or trainling
white spaces so you will need to use s.trim() method before
performing any string operation
Try following code.