Hanif wrote:
> Hi folks,
> I am trying to use the split on a String, the result returns an
> array of Strings. I always get the first String in the array as an
> empty string.
>
> Please note that the StringTokenizer did not work, but with using
> split i am very close to the number of token i want.
>
> Any Ideas !
>
> Thanks a lot
> Hanif
>
>
---------------------------------------------------------------------------
> Here is some code that would illustrate the problem:
>
> String
> data="XXXX:aaaaaaa\nbbbbbbb\nXXXX:cccccc\nddddd\nX XXX:eeeeee\nffffff\n";
>
> String t_data=data.trim();
> String[] TOKENS = t_nodeData_.split("XXXX:");
No, this is wrong, and in two (some might say three) ways. Do it this way:
String[] tokens = t_data.split(":");
And the variable "t_nodeData_" appears nowhere in your code. You must always
post the actual code (copy and paste), not something you made up for your
newsreader.
And avoid the use of ALL UPPERCASE for ordinary variable names.
FInally, if you might have one or more empty final fields that you want
represented in the resulting array, do it this way:
String[] tokens = t_data.split(":",-1);
--
Paul Lutus
http://www.arachnoid.com