File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Find a pattern in String

 
Shikha Sarna
Greenhorn
Posts: 3
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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