Code Comments
Programming Forum and web based access to our favorite programming groups.I have been really struggling with regular expressions, can anyone tell me how I would split up a string around the spaces and newlines. Basically I want every word to be put into a string array but not the spaces (which is what I am getting now). -- Eps
Post Follow-up to this messageburchill wrote: > I have been really struggling with regular expressions, can anyone tell > me how I would split up a string around the spaces and newlines. > Basically I want every word to be put into a string array but not the > spaces (which is what I am getting now). > > -- > Eps Sounds like you need to use the StringTokeniser class
Post Follow-up to this messageAlso you can collapse white space to a single space first.
string.replaceAll("\\s+"," ").split(" ")
This will return a String array of the tokens
Post Follow-up to this message"Andrew McDonagh" <news@andrewcdonagh.f2s.com> wrote in message news:cq7bop$fa1$1@news.freedom2surf.net... > burchill wrote: > > Sounds like you need to use the StringTokeniser class Not the best advice. From the docs: "StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone sing this functionality use the split method of String or the java.util.regex package instead." See my other post.
Post Follow-up to this message<klynn47@comcast.net> wrote in message
news:1103576113.209705.255230@f14g2000cwb.googlegroups.com...
> Also you can collapse white space to a single space first.
>
> string.replaceAll("\\s+"," ").split(" ")
> This will return a String array of the tokens
>
Why add the extra step?
string.split("\\s+");
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.