1

So I want to search through a string to see if it contains the substring that I'm looking for. This is the algorithm I wrote up:

    //Declares the String to be searched
    String temp = "Hello World?";

    //A String array is created to store the individual 
    //substrings in temp
    String[] array = temp.split(" ");

    //Iterates through String array and determines if the
    //substring is present
    for(String a : array)
    {
        if(a.equalsIgnoreCase("hello"))
        {
            System.out.println("Found");
            break;
        }
        System.out.println("Not Found");
    }

This algorithm works for "hello" but I don't know how to get it to work for "world" since it has a question mark attached to it.

Thanks for any help!

Christian Kuetbach
  • 15,850
  • 5
  • 43
  • 79
LTH
  • 1,779
  • 3
  • 19
  • 21

4 Answers4

6

Take a look: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#contains(java.lang.CharSequence)

String.contains();

To get a containsIgnoreCase(), you'll have to make your searchword and your String toLowerCase().

Take a look at this answer: How to check if a String contains another String in a case insensitive manner in Java?

return s1.toLowerCase().contains(s2.toLowerCase());

This will also be true for: war of the worlds, because it will find world. If you don't want this behavior, youll have to change your method like @Bart Kiers said.

Community
  • 1
  • 1
Christian Kuetbach
  • 15,850
  • 5
  • 43
  • 79
  • `.contains()` is casesensetive, while OP uses `equalsIgnoreCase`. http://stackoverflow.com/questions/86780/is-the-contains-method-in-java-lang-string-case-sensitive might be usefull – Vladimir Nov 28 '11 at 10:06
  • 1
    You are right. But at the same location, you can fin the method toLowerCase(). But I'll fix my answer. – Christian Kuetbach Nov 28 '11 at 10:09
  • 1
    this is faulty in the case of `temp="hell worldie"` either `hell` or `worldie` (depending if `s1` is the user controlled string or the input) will produce true. – Ofir Farchy Nov 28 '11 at 10:21
  • Yes, but it was asked for that, I think: 'This algorithm works for "hello" but I don't know how to get it to work for "world" since it has a question mark attached to it.' – Christian Kuetbach Nov 28 '11 at 10:54
4

Split on the following instead:

"[\\s?.!,]"

which matches any space char, question mark, dot, exclamation or a comma (add more chars if you like).

Or do a temp = temp.toLowerCase() and then temp.contains("world").

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
1

You dont have to do this, it's already implemented:

IndexOf and others

Mihalis Bagos
  • 2,500
  • 1
  • 22
  • 32
0

You may want to use :

String string = "Hello World?";
boolean  b = string.indexOf("Hello") > 0;         // true

To ignore case, regular expressions must be used .

b = string.matches("(?i).*Hello.*");

One more variation to ignore case would be :

// To ignore case
b=string.toLowerCase().indexOf("Hello".toLowerCase()) > 0 // true
Sandeep Pathak
  • 10,567
  • 8
  • 45
  • 57