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. "Alex Zorin" <> writes:

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


    Because, as you argue yourself, the method would just be a
    specialization of the general indexOf().

    It's not useful to add specialized cases on top of generalized
    methods; that only leads to library bloat.
     
    Tor Iver Wilhelmsen, Aug 1, 2004
    #2
    1. Advertising

  3. Alex Zorin

    sks Guest

    "Alex Zorin" <> wrote in message
    news:...
    > Well, I'm not having trouble or anything, but I'm wondering why the String
    > class doesnt have a contains method?


    This is in 1.5
     
    sks, Aug 1, 2004
    #3
  4. Alex Zorin

    tom bender Guest

    Intersting idea, but there may be a semantic difference which emans it
    doesn't warrent it being made a contains method.

    Alex Zorin wrote:

    > 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?
    >
    > }
    >
    >
     
    tom bender, Aug 1, 2004
    #4
  5. Alex Zorin

    Alan Moore Guest

    On 01 Aug 2004 14:27:10 +0200, Tor Iver Wilhelmsen
    <> wrote:

    >"Alex Zorin" <> writes:
    >
    >> Well, I'm not having trouble or anything, but I'm wondering why the
    >> String class doesnt have a contains method?

    >
    >Because, as you argue yourself, the method would just be a
    >specialization of the general indexOf().
    >
    >It's not useful to add specialized cases on top of generalized
    >methods; that only leads to library bloat.


    In jdk5.0, String has the new method: contains(CharSequence cs). So,
    while it's still just a special case of indexOf(String s), it also
    works with StringBuffer, StringBuilder, CharBuffer, and any other
    class that implements CharSequence. Kind of a specialization and a
    generalization at the same time.
     
    Alan Moore, Aug 1, 2004
    #5
  6. Alex Zorin

    tom bender Guest

    Not that I can find.
    sks wrote:
    > "Alex Zorin" <> wrote in message
    > news:...
    >
    >>Well, I'm not having trouble or anything, but I'm wondering why the String
    >>class doesnt have a contains method?

    >
    >
    > This is in 1.5
    >
    >
     
    tom bender, Aug 1, 2004
    #6
  7. Alex Zorin

    sks Guest

    sks, Aug 1, 2004
    #7
    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:
    648
  2. Alex Zorin
    Replies:
    1
    Views:
    467
    Michal Dzirba
    Aug 1, 2004
  3. Bruce .J Sam
    Replies:
    2
    Views:
    584
    Bruce .J Sam
    Apr 3, 2005
  4. Pawel_Iks
    Replies:
    3
    Views:
    536
    terminator
    Jul 31, 2007
  5. Abby Lee
    Replies:
    5
    Views:
    419
    Abby Lee
    Aug 2, 2004
Loading...

Share This Page