This week's giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Production-Ready Serverless (Operational Best Practices) and have Yan Cui on-line!
See this thread for details.
Win a copy of Production-Ready Serverless (Operational Best Practices) this week in the Cloud/Virtualization forum!
  • 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
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

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
Posts: 23820
140
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
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...
 
Oh, sure, you could do that. Or you could eat some pie. While reading this tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!