Gadi A
Gadi A

Reputation: 3539

Java split string on empty delimiter returns empty string at the beginning?

I want to split a string to single characters. So I do:

"abcd".split("");

But this yields:

["", "a", "b", "c", "d"]

The first empty string is not something I'm used to when doing the same in other languages (e.g. Ruby). What is the logic behind it?

Upvotes: 2

Views: 1827

Answers (1)

Colin D
Colin D

Reputation: 5661

Why are you using String.split() for this? You might be better served using String.toCharArray().

I know one will return you an array of Strings while the other will give you an array of chars. Since you want each character separately, I am assuming this doesn't matter to your code.

Upvotes: 4

Related Questions