File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Gus Chesterson
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20823
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can ask the regular expression to grab all the delimiters that are together as one delimiter... try "\\W+" as the regex.

Henry
 
Gus Chesterson
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic