Re: Compare part of a string
Have you read through the API for java.lang.String?
db
Re: Compare part of a string
Yep, unfortunately i dont understand it, being so new to java :)
Im on a "dicphering other code" state, to figure out what to do. So i was hoping to get a bit of code I could play with, that would help a lot.
Re: Compare part of a string
For this you can use the method .contains() from the string class. What it does is takes a sequence of chars and compares them to a string. If that string has the char sequence it returns true.
String (Java 2 Platform SE 5.0)
To make it case insensitive you should look into a method that allows you to control to case of the Strings.
Re: Compare part of a string
I'll help you along, but you have to write your own code. To ignore case, you would convert both Strings to either upper or lower case -- String has methods for that. And which method in the API do you think might help you find whether one String contains another String?
db
Re: Compare part of a string
Quote:
Originally Posted by
jhuber151
For this you can use the method .contains() from the string class. What it does is takes a sequence of chars and compares them to a string. If that string has the char sequence it returns true.
String (Java 2 Platform SE 5.0)
To make it case insensitive you just add on a toLowerCase() to both strings, so it would look similar to
Code:
String checkIn = "The cat ran in the house";
String compareTo = "cat";
boolean contains = checkIn.toLowerCase().contains(compareTo.toLowerCase());
System.out.println(contains);
jhuber151, please don't spoonfeed. Give people a chance to learn.
db
Re: Compare part of a string
"contains = checkIn.toLowerCase"
Brilliant. I had found a way, but this is way more clean and simple. Thx a lot.
Re: Compare part of a string
you can also use Code:
message.indexOf("word")
Just another possibility you might consider.
Re: Compare part of a string
Quote:
Originally Posted by
bob 6
you can also use
Code:
message.indexOf("word")
Just another possibility you might consider.
No, you can't. Read the question in the first post, carefully.
db