• 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
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

help with String?

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I have a String sentence (for ex. "Welcome back back back home home!!!"), I want to remove the redundancy in the statement ("Welcome back home!")

Is there any method in String, or StringBuffer class help me do that?

Regards,
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use a tokenizer and search for duplicates.

I believe the class name is StringTokenizer.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It can also be done with regular expressions and the replaceAll() method of String class, if you're somewhat familliar with regular expressions.
Although I have to admit, this regex doesnt preserve punctuation that occurs after a repeated word i.e.:
"back, back, back," --> "back"

I suspect that if you play around with it a little, that can be fixed too.

[edit: found a bug in my regex]
[ March 25, 2006: Message edited by: Garrett Rowe ]
 
Fire me boy! Cool, soothing, shameless self promotion:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!