3

With Java 1.5, the contains(CharSequence s) method was added to the String class. This method

Returns true if and only if this string contains the specified sequence of char values.

How would you do this in versions of Java prior to 1.5, specifically in version 1.4.2?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
user218420
  • 33
  • 1
  • 1
  • 3

1 Answers1

15

To get the same effect as String.contains(substring) in Java 1.4, you can use the following snippet:

String.indexOf(substring) != -1
Paul Wagland
  • 27,756
  • 10
  • 52
  • 74
  • contains() does exactly this, so no difference. – Bozho Nov 25 '09 at 09:14
  • 1
    I have updated the answer to explain what I think the asker is asking... in particular, contains doesn't exist in Java 1.4, and that is, by my understanding of the question, where tasmohan is trying to compile. – Paul Wagland Nov 25 '09 at 09:15
  • yes.. exactly i tried to asked what you thought.thanks for answer. – user218420 Nov 25 '09 at 10:18
  • @Paul - there is one minor nit. The `contains` method takes any `CharSequence` as an argument, but `indexOf` requires a `String` (or a `char`). – Stephen C Nov 25 '09 at 10:57
  • 1
    @Stephen - yes, you are correct, in the case where you really want to use a 'CharSequence' you should do 'substring.toString()'. – Paul Wagland Nov 25 '09 at 12:00
  • Updated the answer again, based on the question now clearly asking what was intended. – Paul Wagland Dec 02 '09 at 08:39