aspose file tools*
The moose likes Beginning Java and the fly likes Find a pattern in String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Find a pattern in String" Watch "Find a pattern in String" New topic
Author

Find a pattern in String

Shikha Sarna
Greenhorn

Joined: Aug 01, 2005
Posts: 3
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

Joined: Jun 09, 2003
Posts: 4632
here's a couple of ways

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

Joined: Aug 01, 2005
Posts: 3
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.
 
 
subject: Find a pattern in String