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

parse String

 
Andy Hahn
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using JDK 1.4 and would like to know a simpe way to check if a String contains part of another String.

For example, how can I tell if the String "and" is found anywhere in the String "Stand"?

Is there a method on the String class for this? If it involves regular expressions, what would that be?

Thanks.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could try "indexOf"
 
Adam Richards
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like you may unaware of the comprehensive API documentation available online. The documentation (cleverly named "Javadocs") covers the methods and properties of all the core Java classes & interfaces. For exampleclick here to see the documentation for the String class
 
Andy Hahn
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
indexOf looks like it is teh way to go.

I am familiar with the javadoc. I was just looking for advice on which particular String method to use. The javadoc did not provide enough information for me to make the best decision on which of the many methods to use.

Thanks for the comments.
 
Santosh Pasupuleti
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andy,
You can use the folowing example:
 
Santosh Pasupuleti
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But, the above code will work if you are searching for "AND" in "stand".
indexOf will return -1 (not found) if there is case mismatch.

So, you will have to introduce the toLowerCase() or toUpperCase() methods of String class.

Take a look at the following code:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic