• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic