3
String test="I am preparing for OCPJP";
String[] tokens=test.split("\\S");
System.out.println("length:"+tokens.length);
for(String s:tokens) {
  System.out.print("["+s+"]");
}
System.out.println();

output:

length:16
[][ ][][ ][][][][][][][][][ ][][][ ]

and now I changed split(regex) to split(regex,limit)

output:

length:21
[][ ][][ ][][][][][][][][][ ][][][ ][][][][][]

could you tell me why is this result different?Thanks a lot!

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
leometal
  • 29
  • 5
  • what is the value of the limit here? – Kick Buttowski May 15 '14 at 22:39
  • I voted to close this question as duplicate but if you feel that duplicate doesn't answer your question feel free to inform me with information about which part is not covered and I will either improve duplicate or reopen your question. – Pshemo May 15 '14 at 22:53
  • yes,it is duplicated but thank you all the same and I will search more carefully before I asked question later – leometal May 15 '14 at 23:12

1 Answers1

4

The Javadocs for the 2-arg overload of split state:

If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

The Javadocs for the 1-arg overload of split state:

This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

And the 1-arg, no limit overload is equivalent to a limit of 0. With a non-zero limit, trailing empty strings are no longer discarded. Those 5 trailing empty strings that are no longer discarded correspond to the non-spaces found in "OCPJP".

rgettman
  • 176,041
  • 30
  • 275
  • 357
  • what is the value of non-zero limit for example? – Kick Buttowski May 15 '14 at 22:38
  • @KickButtowski To get at a length of at least 21, the `limit` must have been negative (no limit) or at least `21`. – rgettman May 15 '14 at 22:39
  • thank you for your answer but I still not understand that why the 1-arg split method discarded the last string **OCPJP** only for 16 split parts not 21. – leometal May 15 '14 at 22:41
  • @rgettman I kind of learned what you said but i am not clear about it. Could you please explain more? – Kick Buttowski May 15 '14 at 22:42
  • so 16 is all the word without counting blank parts, and 21 is all the words with counting blank parts. Did I get it right? – Kick Buttowski May 15 '14 at 22:44
  • @KickButtowski Yes, there were 21 elements, and the last 5 were all empty. After they were discarded, 16 were left. I've also added to my answer. – rgettman May 15 '14 at 22:45
  • @Leometal "OCPJP" is five consecutive delimiters (non-whitespace) at the end of the string, with four empty strings in between the letters, and one empty string after the letters. – rgettman May 15 '14 at 22:46
  • 16 seems just ignore the last **OCPJP** string,you could reference the **[]** display – leometal May 15 '14 at 22:48
  • 1
    @Leometal The last OCPJP string isn't ignored; it's that the last 5 empty strings were discarded, so they don't show up in the returned array. – rgettman May 15 '14 at 22:49
  • @rgettman OK,thank you for your remind this detail in the doc for it.Thanks! – leometal May 15 '14 at 22:56