• Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting String to String []  RSS feed

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I convert a String to a String array? I am grabbing a String from a JTextField; this value has to go into a method that needs it in String[] format. Thanks
 
Jim Thompson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about this:
SearchString = searchField.getText(); //from JTextField
String[] strings = SearchString.split("\\s"); //split on space
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, split() is probably exactly what you want. If indeed splitting on whitespace is what you need. Note that you might want to split on "\\s+" instead. Otherwise if you have any consecutive whitespaces, like say a space followed by a newline (a common occurance), your array would have an extra empty string "" representing what's between the space and the newline.
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you simply want to pass in the entire string as a single-element array:

You could do the above in one statement, but this shows how you allocate the array and set the value.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!