Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Best way of matching substring

 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya, that is the one I missed.
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not that there is anything wrong with indexOf(), but 1.4 added a regex method to String:

public boolean matches(String regex)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic