This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Find a pattern in String

 
Shikha Sarna
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I need to replace a particular pattern in a String, I can use the replaceAll() method and that pattern will be replaced everywhere. But what should I do if I just need to print how many times that particualr pattern occurred in the String?
Please give me pointers in this regards.
Thanks
[ August 02, 2005: Message edited by: Shikha Sarna ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here's a couple of ways

(string.length() - string.replaceAll(...).length())/word.length()
string.split(..,-1).length - 1
 
Shikha Sarna
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Dunn:
here's a couple of ways

(string.length() - string.replaceAll(...).length())/word.length()
string.split(..,-1).length - 1


Thanks a lot.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic