Jump to content
  • Advertisement
Sign in to follow this  
ErUs

[java] String Comparison

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!