GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes parse String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "parse String" Watch "parse String" New topic
Author

parse String

Andy Hahn
Ranch Hand

Joined: Aug 31, 2004
Posts: 225
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

You could try "indexOf"


[My Blog]
All roads lead to JavaRanch
Adam Richards
Ranch Hand

Joined: Nov 03, 2005
Posts: 135
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

Joined: Aug 31, 2004
Posts: 225
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

Joined: Aug 10, 2005
Posts: 97
Andy,
You can use the folowing example:
Santosh Pasupuleti
Ranch Hand

Joined: Aug 10, 2005
Posts: 97
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:
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: parse String