2

I would like to convert string to string array.. I have done something like this:

String name = "name";
String[] letters = name.split("(?<=.)");
System.out.println("letters = " + Arrays.toString(letters));

But now I want something like this:

String name = "John Mark Lester Andrew Todd";

To print array like this:

[John, Mark, Lest, Andrew, Todd]

So I want to look for spaces in my string and then put them into a string array. Do you have ideas to do this? Help is much appreciated. Thanks.

1
  • 5
    this print exactly what do you want System.out.println(Arrays.toString(name.split(" "))); Commented Jan 9, 2014 at 5:54

6 Answers 6

8

You can simply split() your string on whitespace.

String[] words = name.split(" ");
Sign up to request clarification or add additional context in comments.

Comments

2

You can use split() and create an array.

String name = "John Mark Lester Andrew Todd";
String[] names=name.split(" ");

Comments

2

What ever criteria or letter on the basis of which a string is to be split,that need to be provided under

  string.split(" ");

so,u can try using these updated code:::

 String name = "John Mark Lester Andrew Todd";
 String[] letters = name.split(" ");
 System.out.println("letters = " + Arrays.toString(letters));

and the ouput will be::

  letters = [John, Mark, Lester, Andrew, Todd]

And for better understanding for splitting the strings in java,you can go to this link. http://www.tutorialspoint.com/java/java_string_split.htm

2 Comments

I think its better to use "\\s+" instead of " ".
We've all been there bro.. :)
1

Perhaps you can use StringTokenizer

2 Comments

You forgot to mention this about StringTokenizer - StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
My bad. The split method is even mentioned in the docs. Anyway just pointed out that this is also possible. It IS deprecated though.
1

You can use :

String values=Arrays.toString(name.split(" "));

or

String values[] = name.split("\\s")

Comments

1

The most flexible is to split on all whitespace:

String names = "John  \t\t  Mark  Lester \n  Andrew Todd \r";

System.out.println(Arrays.toString(names.split("\\s+")));

Outputs:

[John, Mark, Lester, Andrew, Todd]

\\s+ is regex for "any length of any whitespace characters". Similarly if you only care about spaces:

String names = "John  Mark  Lester   Andrew Todd  ";

System.out.println(Arrays.toString(names.split(" +")));

(Outputs same as above.)

Of course both of those will split single spaces too.

8 Comments

you changed the actual value of names, and OP want the output in format [John, Mark, Lest, Andrew, Todd]. but I agree the use of "\\s+" instead of " "
@RafaEl All I did was mangle it to show the regex handles any whitespace. That would be a stupid reason to downvote. The regex splits the OP's single space input too.
then you might want to explain in the answer, why you add \t\t, \n, \r there.
@RafaEl Because they are whitespace characters. \t is a tab, \n is a new line and \r is a carriage return.
@RafaEl I think it's pretty clear what the regex does. I think somebody just had a vacuous reason for voting. Thanks for trying to help.
|

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.