This content has been marked as final. Show 9 replies
EJP wrote:I intend to do this ...
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 want value separation with comma
Input: String str=",,,";
Output: array of length 4 with each element being the empty string
Input: String str="value1,value2,value3,value4";
Output: array of length 4 with each element being values
element = value1
element = value2
element = value3
element = value4
How do I do it ?
EJP wrote:Have looked at this method ...still not sure what needs to be sent for "int" argument .
Have a good look at the String.split(String, int) method and the various possibilities for the second 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 ?
EJP wrote:It says
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.
+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.
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.