[java] String Comparison

Started by
5 comments, last by Lucidquiet 18 years, 7 months ago

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
-www.freewebs.com/tm1rbrt -> check out my gameboy emulator ( worklog updated regularly )
Advertisement
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.
Quote:Original post by ErUs
my 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.
i changed them both toLower() and used with compareTO() and now it seems to work allright :)

thanks guys.

[edit]

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?
-www.freewebs.com/tm1rbrt -> check out my gameboy emulator ( worklog updated regularly )
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...
-----The next statement is not true!The previous statement is true!
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-
"Education is when you read the fine print; experience is what you get when you don't." -Pete Seegerwww.lucid-edge.net
Quote:Original post by ErUs
i changed them both toLower() and used with compareTO() and now it seems to work allright :)

thanks guys.

[edit]

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]
"Education is when you read the fine print; experience is what you get when you don't." -Pete Seegerwww.lucid-edge.net

This topic is closed to new replies.

Advertisement