I'm writing following program to separate characters from a string and assign it to an array.
public class Str {
public static void main(String[] args) {
String str = "hello";
String[] chars = str.split("");
for (int i = 0; i < chars.length; i++) {
System.out.println(i + ":" + chars[i]);
}
}
}
The output I'm getting is:
0:
1:h
2:e
3:l
4:l
5:o
I'm getting an empty string as the first element of the array. I was expecting the output to be without empty string and the length of chars array to be 5 instead of 6. Why empty char is coming after splitting this String?
See Question&Answers more detail:os