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

A

Alex Zorin

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?

}
 
M

Michal Dzirba

[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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top