java.Contains(String search) method to be made in Java API ?

Discussion in 'Java' started by Alex Zorin, Aug 1, 2004.

  1. Alex Zorin

    Alex Zorin Guest

    Well, I'm not having trouble or anything, but I'm wondering why the String
    class doesnt have a contains method?

    eg

    public boolean contains(String str)

    So you could write

    if(myString.contains("poo" ) {
    / / Do whatever
    }

    Right now the only way to do this (or easiest at least) is to use
    indexOf(String s) and see if the result isn't '-1'. So I propose:


    public boolean contains(String full, String searched) {

    if(full.indexOf(searched) != -1)
    return true;

    else { return false; }

    }


    Of course, you could make it so it extends the String class, but just an
    example? I think this form of String manipulation would be most useful in
    many cases..

    Anyone else agree it'd be a useful addition?

    }
     
    Alex Zorin, Aug 1, 2004
    #1
    1. Advertising

  2. [cut]

    > Of course, you could make it so it extends the String class, but just an


    If by extending you ment subclassing - id be hard as Strings are final.

    > example? I think this form of String manipulation would be most useful in
    > many cases..


    And - i see nothing wrong in using indexOf, or ragexsps for more
    complicated requests.
    Adding such a feature would sound as an overkill - for me.

    Greetings
    Michal.
     
    Michal Dzirba, Aug 1, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Dyer
    Replies:
    5
    Views:
    651
  2. Alex Zorin
    Replies:
    6
    Views:
    88,144
  3. Bruce .J Sam
    Replies:
    2
    Views:
    585
    Bruce .J Sam
    Apr 3, 2005
  4. Pawel_Iks
    Replies:
    3
    Views:
    537
    terminator
    Jul 31, 2007
  5. Abby Lee
    Replies:
    5
    Views:
    437
    Abby Lee
    Aug 2, 2004
Loading...

Share This Page