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.
Simply Use the .tocharArray() method in java
This should work just fine in JAVA 8
Assuming you really want an array of single-character strings (not a
char[]
orCharacter[]
)1. Using a regex:
The zero width negative lookahead prevents the pattern matching at the start of the input, so you don't get a leading empty string.
2. Using Guava:
String array = array of characters ?
Or do you have a string with multiple words each of which should be an array element ?
String[] array = yourString.split(wordSeparator);
In java 8, there is a method with which you can do this: toCharArray():
This results to the following array:
Method
String.split
will generate empty 1st, you have to remove it from the array. It's boring.To start you off on your assignment,
String.split
splits strings on a regular expression, this expression may be an empty string:Yields the array:
Getting rid of the empty 1st entry is left as an exercise for the reader :-)
Note: In Java 8, the empty first element is no longer included.