problem splitting a string

Discussion in 'Java' started by shadykazan@gmail.com, Feb 9, 2007.

  1. Guest

    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 ?
     
    , Feb 9, 2007
    #1
    1. Advertising

  2. Chris Dollin Guest

    wrote:

    > 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.

    --
    Chris "electric hedgehog" Dollin
    "No-one here is exactly what he appears." G'kar, /Babylon 5/
     
    Chris Dollin, Feb 9, 2007
    #2
    1. Advertising

  3. Guest

    On Feb 9, 10:51 am, Chris Dollin <> wrote:
    > wrote:
    > > 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.
    >
    > --
    > Chris "electric hedgehog" Dollin
    > "No-one here is exactly what he appears." G'kar, /Babylon 5/


    woops thanks man
    sorry it was a stupid mistake !!
     
    , Feb 9, 2007
    #3
  4. On Fri, 09 Feb 2007 00:40:34 -0800, shadykazan wrote:

    > 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.
     
    Stefan Schmiedl, Feb 9, 2007
    #4
  5. Sanjay Guest

    wrote:
    > 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);
     
    Sanjay, Feb 13, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page