aspose file tools*
The moose likes Beginning Java and the fly likes need help with String split() - two spaces in a row Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "need help with String split() - two spaces in a row" Watch "need help with String split() - two spaces in a row" New topic
Author

need help with String split() - two spaces in a row

Gus Chesterson
Greenhorn

Joined: Jun 05, 2005
Posts: 2
I am trying to seperate words in a sentence by using String's split() and supplying [^\\w] as the reg ex. The regular expression is for non-word characters. Problem is that if you have a phrase like this "I Like" where there are two spaces in a row, it returns {"I", " "} as the words... I want it to skip over that second space and grab the next word, "Like". How do I do that? Seems to work with StringTokenizer but I noticed that use of StringTokenizer is discouraged.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

You can ask the regular expression to grab all the delimiters that are together as one delimiter... try "\\W+" as the regex.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Gus Chesterson
Greenhorn

Joined: Jun 05, 2005
Posts: 2
No luck, it just skips one space and then grabs the next space as if it were a word. Anything else I can try?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Um, I don't suppose you maybe mixed Henry's answer into your previous answer, did you? Maybe with a regex like "[\\W+]" or "[^\\W+]"? Those certainly would give you some strange answers. Henry's answer of "\\W+" was intende dto be the complete regex, and it really ought to work. If it doesn't - could you please show the exact code you're using to perform the split? Thanks...


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need help with String split() - two spaces in a row