I have a string="name";
I want to convert into a string array.
How do I do it?
Is there any java built in function? Manually I can do it but I'm searching for a java built in function.
I want an array where each character of the string will be a string. like char 'n' will be now string "n" stored in an array.
Splitting an empty string with String.split() returns a single element array containing an empty string. In most cases you'd probably prefer to get an empty array, or a null if you passed in a null, which is exactly what you get with org.apache.commons.lang3.StringUtils.split(str).
Another option is google guava Splitter.split() and Splitter.splitToList() which return an iterator and a list correspondingly. Unlike the apache version Splitter will throw an NPE on
null
:If you want a list rather than an iterator then use Splitter.splitToList().
here is have convert simple string to string array using split method.
Output
I guess there is simply no need for it, as it won't get more simple than
Of course if you insist, you could write:
[Edit] If you want single-letter Strings, you can use:
Unfortunately s[0] will be empty, but after this the letters n,a,m,e will follow. If this is a problem, you can use e.g. System.arrayCopy in order to get rid of the first array entry.