This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Best way of matching substring Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Best way of matching substring" Watch "Best way of matching substring" New topic
Author

Best way of matching substring

William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

How should I find "red" in "This is a sentence with the word red in it."? My first inclination is to do a step by step character matching loop. But that seems a bit non-java-ish. I don't see any help in the String api. So do I have to use something like Matcher class?


Please ignore post, I have no idea what I am talking about.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You could do this with a regular expression, as I think you've worked out. Alternatively (and more easily) you could use this method of the String class:


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

Ya, that is the one I missed.
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
Not that there is anything wrong with indexOf(), but 1.4 added a regex method to String:

public boolean matches(String regex)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Best way of matching substring
 
Similar Threads
Collections - patter matching program
Set font attributes of text in DefaultMutableTreeNode?
oracle on unix
Difficuties to use Breakpoint from Eclipse
import jar in eclipse //