2

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?

4

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I was more interested in the strange behavior, but you are correct; that is a better way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.