Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

comparing Strings

 
Andy Hahn
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're using Java 1.5, there's a new contains(CharSequence s) method in the String class.
 
Andy Hahn
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're comfortable with regular expressions, there's also a matches method in 1.4.
 
ryan zhang
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think it is easy to use String.indexOf("xxx"); to find whether a string contains another string.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Andy Hahn
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent! Thanks much everyone.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic