String.split("") puts null in the first index
So I have absolutely no idea why this is happening, hopefully someone can help me out. I'm a novice programmer, and lately I've noticed that whenever i use String.split() with "" as a delimiter, the first index will be null and the second index will contain the first element, and so on. Here's an example of some output i've been seeing:
Code :
public class wtf
{
public static void main(String[] args)
{
String mystring = "hello";
String[] myarray = mystring.split("");
for(int i=0; i<myarray.length;i++)
System.out.println(i + ": "+ myarray[i]);
}
}
OUTPUT:
0:
1: h
2: e
3: l
4: l
5: o
Re: String.split("") puts null in the first index
The first element in the array looks like an empty String, not null.
I don't understand regular expressions to be able to tell you what an empty regexp String will do.
Re: String.split("") puts null in the first index
use trim before apply split function. let me know if it doe'st not solve your problem
Re: String.split("") puts null in the first index
I don't see a blank in the String that the OP posted. Do you? What would trim do with no spaces?
Re: String.split("") puts null in the first index
this is probably too late to get another response, but kB18001 I tried String.trim() first but to no avail, I get the same output