[java] String Comparison

This topic is 5163 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

if( message.contains( this.getNick() )) {
String substr = message.substring( 0, (this.getNick().length()));
String botname = this.getNick();
if( substr.equals( botname ) ) {
message = message.substring( this.getNick().length() );
} else {
message = message.replace( this.getNick(), "you" );
}
ChanMsg(channel,message);
}


If the start of message == botname, ChanMsg() should be called with a substring of message leaving out the botname at the start. else all instances of botname in message should be replaced with you. - my problem is that when substr and botname = the same thing ( i have debugged and checked ) it still does the else code :(. can anyone help plz

Share on other sites
Use the String class compareTo function or equals function I believe. I tend to use the compareTo function more

if(firstString.compareTo(secondString) == 0)
{
// strings are the same
}

Look at a previous post.... this was answered recently. == on string compares the actual string objects for equality, not the string contents.

Share on other sites
Quote:
 Original post by ErUsmy problem is that when substr and botname = the same thing ( i have debugged and checked ) it still does the else code :(.

Hmm that's weird. Are you sure they're really equal? Note that .equals is case-sensitive, so if one string is "joe" and the other string is "Joe", then it will return false.

Share on other sites
i changed them both toLower() and used with compareTO() and now it seems to work allright :)

thanks guys.

also can anyone point me to a tutorial that shows me how i can write a whole object/class to a file and read it again?

Share on other sites
look at www.onjava.com... there are tutorials for objectpersistence... search for jdo and you will find some tutorials... search for jdo in google and you will get some comparisons because there are some more tools for such activities...

Share on other sites
Or just declare your top most super class as Serializable and use an ObjectOutputStream, and ObjectInputStream. The only draw back is that you can't searialize static member variables -- and there is little to no way around this. My philosophy is just not to use static member variable, anyway, so ObjectStream (output/input) are still options for me.

See java.io.ObjectInputStream, and java.io.ObjectOutputStream in the SDK, and check the Java Tutorial. Also most of the java.util.* classes are already Serializable, so you don't have to worry too much there.

L-

Share on other sites
Quote:
 Original post by ErUsi changed them both toLower() and used with compareTO() and now it seems to work allright :)thanks guys.also can anyone point me to a tutorial that shows me how i can write a whole object/class to a file and read it again?

Also there is a Comparator in the String class, that is case insensitive. So you could also write something like this:

if (String.CASE_INSENSITIVE_ORDER.compareTo(lhs, rhs) == 0) {   // ... do something here}

L-

Edit: Forgot the .compareTo method call.

[Edited by - Lucidquiet on August 29, 2005 7:22:14 PM]

• Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 13
• 18
• 15
• 9
• 9