wood burning stoves 2.0*
The moose likes Java in General and the fly likes comparing Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "comparing Strings" Watch "comparing Strings" New topic
Author

comparing Strings

Andy Hahn
Ranch Hand

Joined: Aug 31, 2004
Posts: 225
Hi, I would like to know if there is an easy way to compare the contents of one String with another String.

For example, I have a List containing the following Strings:
"Minnesota"
"Wisconsin"
"Indiana"
"Michigan"


I would like to return a list of states that contain the String "in", while ignoring case.

Thanks!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

If you're using Java 1.5, there's a new contains(CharSequence s) method in the String class.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Andy Hahn
Ranch Hand

Joined: Aug 31, 2004
Posts: 225
I am actually using 1.4.2. I did however find the following article:
http://java.sun.com/docs/books/tutorial/java/data/comparestrings.html
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

If you're comfortable with regular expressions, there's also a matches method in 1.4.
ryan zhang
Greenhorn

Joined: Aug 09, 2005
Posts: 3
Originally posted by Andy Hahn:
Hi, I would like to know if there is an easy way to compare the contents of one String with another String.

For example, I have a List containing the following Strings:
"Minnesota"
"Wisconsin"
"Indiana"
"Michigan"


I would like to return a list of states that contain the String "in", while ignoring case.

Thanks!

i think it is easy to use String.indexOf("xxx"); to find whether a string contains another string.
ryan zhang
Greenhorn

Joined: Aug 09, 2005
Posts: 3
i think it is easy to use String.indexOf("xxx"); to find whether a string contains another string.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
if you want to do a case insensitive search you will need to convert your original string to lower or upper case
e.g.

will return a positive value.


Joanne
Andy Hahn
Ranch Hand

Joined: Aug 31, 2004
Posts: 225
Excellent! Thanks much everyone.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: comparing Strings
 
Similar Threads
TreeSet vs Collection.Sort / Array.sort for Strings
I don't see an advantage of Generics of Java 5 here!
Right way to concat Strings and then substring them ?
Object and String
Extracting characters from a String