problem splitting a string

S

shadykazan

hi im trying to split a string into an array using string.split
my delimiter is the null character '\u0000'
the problem is that when there are 2 or more consecutive null
characters at the end of the string,
they don't get split and java ignores them,but if in the middle of the
string it works fine.
i have tried inserting a space between each 2 consecutive nulls

data.replaceAll("\u0000{2}","\u0000 \u0000");
String [] array=data.split("\u0000");

but it did not work, im not so good at regular expressions but i think
this should've worked
maybe there is another solution, other then this one or another way to
split the string !
any ideas ?
 
C

Chris Dollin

hi im trying to split a string into an array using string.split
my delimiter is the null character '\u0000'
the problem is that when there are 2 or more consecutive null
characters at the end of the string,
they don't get split and java ignores them,but if in the middle of the
string it works fine.
i have tried inserting a space between each 2 consecutive nulls

data.replaceAll("\u0000{2}","\u0000 \u0000");

You've done a `.replaceAll` on `data` and discarded the result. replaceAll
does not change the subject string -- it can't, since String's are
immutable. It returns the modified string.
 
S

shadykazan

You've done a `.replaceAll` on `data` and discarded the result. replaceAll
does not change the subject string -- it can't, since String's are
immutable. It returns the modified string.

woops thanks man
sorry it was a stupid mistake !!
 
S

Stefan Schmiedl

hi im trying to split a string into an array using string.split
my delimiter is the null character '\u0000'
...
maybe there is another solution, other then this one or another way to
split the string !
any ideas ?

RTFjavadoc on String.split ... it has another parameter to solve exactly
your problem.

s.
 
S

Sanjay

hi im trying to split a string into an array using string.split
my delimiter is the null character '\u0000'
the problem is that when there are 2 or more consecutive null
characters at the end of the string,
they don't get split and java ignores them,but if in the middle of the
string it works fine.
i have tried inserting a space between each 2 consecutive nulls

data.replaceAll("\u0000{2}","\u0000 \u0000");
String [] array=data.split("\u0000");

but it did not work, im not so good at regular expressions but i think
this should've worked
maybe there is another solution, other then this one or another way to
split the string !
any ideas ?

May be this will help
String [] array=data.split("\u0000", -1);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top