Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > java.Contains(String search) method to be made in Java API ?

Reply
Thread Tools

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

 
 
Alex Zorin
Guest
Posts: n/a
 
      08-01-2004
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?

}


 
Reply With Quote
 
 
 
 
Tor Iver Wilhelmsen
Guest
Posts: n/a
 
      08-01-2004
"Alex Zorin" <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
sks
Guest
Posts: n/a
 
      08-01-2004

"Alex Zorin" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
tom bender
Guest
Posts: n/a
 
      08-01-2004
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?
>
> }
>
>

 
Reply With Quote
 
Alan Moore
Guest
Posts: n/a
 
      08-01-2004
On 01 Aug 2004 14:27:10 +0200, Tor Iver Wilhelmsen
<(E-Mail Removed)> wrote:

>"Alex Zorin" <(E-Mail Removed)> 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.
 
Reply With Quote
 
tom bender
Guest
Posts: n/a
 
      08-01-2004
Not that I can find.
sks wrote:
> "Alex Zorin" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>>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
>
>

 
Reply With Quote
 
sks
Guest
Posts: n/a
 
      08-01-2004

"tom bender" <(E-Mail Removed)> wrote in message
news:Lf7Pc.370$(E-Mail Removed)...
> Not that I can find.


http://java.sun.com/j2se/1.5.0/docs/...#contains(java.
lang.CharSequence)


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Poll: Is a Java Method an Instance of the Java Classjava.lang.reflect.Method? Please reply with YES or NO. Paka Small Java 15 02-07-2012 07:04 PM
Rebel XT, made in Japan, made in Thailand jazu Digital Photography 10 12-12-2006 05:11 AM
The method add(...) in java.awt.Container made me in confuse Bruce .J Sam Java 2 04-03-2005 12:24 AM
my dream - a simplified version of java - made from java rfractal30 Java 5 02-21-2005 05:54 PM
java.Contains(String search) method to be made in Java API ? Alex Zorin Java 1 08-01-2004 11:19 AM



Advertisments