• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

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

 
Greenhorn
Posts: 2
  • 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.
 
author
Sheriff
Posts: 23603
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 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
  • 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?
 
Wanderer
Sheriff
Posts: 18671
  • 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...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!