This discussion is archived
9 Replies Latest reply: Jan 27, 2013 3:52 PM by EJP RSS

split not working

user575089 Newbie
Currently Being Moderated
why this split not working ?

String str=",,,";

String[] splitStr=str.split(",");

I expect 4 splitted elemnts here....but its not splitting
  • 1. Re: split not working
    sabre150 Expert
    Currently Being Moderated
    I think it should result in an array of length 4 with each element being the empty string. What are you expecting?
  • 2. Re: split not working
    user575089 Newbie
    Currently Being Moderated
    sabre150 wrote:
    I think it should result in an array of length 4 with each element being the empty string.
    I expect the same. ...but its giving empty array...length zero
  • 3. Re: split not working
    EJP Guru
    Currently Being Moderated
    From the Javadoc: 'trailing empty strings are discarded'. In this case that's all of them. So a zero length array is as expected.
  • 4. Re: split not working
    user575089 Newbie
    Currently Being Moderated
    EJP wrote:
    From the Javadoc: 'trailing empty strings are discarded'. In this case that's all of them. So a zero length array is as expected.
    I intend to do this ...

    I want value separation with comma

    Case 1:

    Input: String str=",,,";

    Output: array of length 4 with each element being the empty string


    Case 2:

    Input: String str="value1,value2,value3,value4";

    Output: array of length 4 with each element being values

    element[0] = value1
    element[1] = value2
    element[2] = value3
    element[3] = value4


    How do I do it ?
  • 5. Re: split not working
    EJP Guru
    Currently Being Moderated
    Have a good look at the String.split(String, int) method and the various possibilities for the second argument.
  • 6. Re: split not working
    user575089 Newbie
    Currently Being Moderated
    EJP wrote:
    Have a good look at the String.split(String, int) method and the various possibilities for the second argument.
    Have looked at this method ...still not sure what needs to be sent for "int" argument .



    str.split("," , *<what_value_here>*) ?


    Please look at my desired input/output I posted earlier ...... Is it possible using split() at all or do I need StringTokenizer to achieve ?
  • 7. Re: split not working
    EJP Guru
    Currently Being Moderated
    Yes it's possible. Look again. Look particularly at what happens when zero is passed, which I quoted above, and which is clearly what you don't want to happen.
  • 8. Re: split not working
    user575089 Newbie
    Currently Being Moderated
    EJP wrote:
    Yes it's possible. Look again. Look particularly at what happens when zero is passed, which I quoted above, and which is clearly what you don't want to happen.
    It says

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

    what is this trailing empty strings will be discarded ?


    anyway I'll try putting zero ...will check if problem resolves.
  • 9. Re: split not working
    EJP Guru
    Currently Being Moderated
    Oh please. Read it all. It says that the method you are calling is equivalent to passing zero to the other one, which causes all trailing strings to be discarded, which is the behaviour you are complaining about, and all of which which you have already been told. So passing zero is the very last thing you should be doing.

    You need to get a lot better at reading documentation than this.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points