Skip to main content

add "public boolean String.contains(char c)"

2 replies [Last post]
monika_krug
Offline
Joined: 2004-10-14
Points: 0

Java 5.0 finally added String.contains(CharSequence s) - that's good, using String.indexOf(String s)>-1 can get pretty annoying. But this (String.indexOf(char c)>-1) still needs to be done for chars (or the char converted to a CharSequence, e.g. String).

An alternative is to make Character implement the CharSequence interface. Sure, there is no real use for calling .charAt, .length or .subSequence on a Character, but that way and together with the autoboxing chars could be passed as parameters where CharSequences are required - e.g. to the String.contains method. It might not be very efficient, though (autoboxing, then searching the String for a CharSequence, instead of just implementing the method as "return indexOf(c)>-1;").

On the other hand, the String.indexOf methods only allow chars and Strings as parameters. Maybe they should allow CharSequences, too.

Monika.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
afreije
Offline
Joined: 2004-10-14
Points: 0

or better:[code]public boolean contains(int codepoint)[/code]

monika_krug
Offline
Joined: 2004-10-14
Points: 0

int codepoint? Can you explain that?

Monika.