Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: contains vs. indexOf

  1. #1
    Junior Member
    Join Date
    May 2009
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default contains vs. indexOf

    Is there any situation where

    if (str1.contains(str2)) {}
    and
    if (str1.indexOf(str2)>=0) ) {}

    might be different?
    A colleague has been changing all occurrences of the 1st form for the 2nd. But I don't see the reason and I can't ask him.


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: contains vs. indexOf

    Firstly welcome to Java Programming Forums

    As far as im aware .contains() is a newer method that is just a specialized case of indexOf()

  3. The Following User Says Thank You to Freaky Chris For This Useful Post:

    mgrootsch (September 21st, 2009)

  4. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: contains vs. indexOf

    First off, contains returns true/false while indexOf will return the index of the search.

    But the magic is in the contains method, you ready for this, its a funny one

        public boolean contains(CharSequence s) {
            return indexOf(s.toString()) > -1;
        }

    // Json

  5. The Following User Says Thank You to Json For This Useful Post:

    mgrootsch (September 21st, 2009)

  6. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: contains vs. indexOf

    Quote Originally Posted by Json View Post
    First off, contains returns true/false while indexOf will return the index of the search.

    But the magic is in the contains method, you ready for this, its a funny one

        public boolean contains(CharSequence s) {
            return indexOf(s.toString()) > -1;
        }

    // Json
    Told you contains was just an specialized implementation of indexOf. Making it better practice to infact use indexOf()

    Chris

  7. #5
    Junior Member
    Join Date
    May 2009
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: contains vs. indexOf

    Better practice, maybe... But hardly worth the trouble of having to check all those differences that light up in CVS (in my opinion).

    Thanks for the replies.
    Marcel

  8. #6
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: contains vs. indexOf

    The methods have different use, if you need to check if the String contains something then use the contains, but if you want to know where in the String it is contained, use the indexOf method.

    // Json