This discussion is archived
3 Replies Latest reply: Jun 24, 2009 1:31 PM by 807588 RSS

Is There a bug on String.split() method?

800429 Newbie
Currently Being Moderated
Hi guys,

I was performing some test with the String.split() method and I found something really odd.

The code below
String text = "TOKEN1:TOKEN2:TOKEN3"
String[] tokens = text.split(":")
produce the result
tokens[0] = TOKEN1
tokens[1] = TOKEN2
tokens[2] = TOKEN3

The code below
String text = ":TOKEN2:TOKEN3"
String[] tokens = text.split(":")
produce the result
tokens[0] = ""
tokens[1] = TOKEN2
tokens[2] = TOKEN3

The code below
String text = ":TOKEN2:"
String[] tokens = text.split(":")
produce the result
tokens[0] = ""
tokens[1] = TOKEN2

Shouldn't it be ?

tokens[0] = ""
tokens[1] = TOKEN2
tokens[2] = ""

Thanks and Regards