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!

2
  • Do you mean that "world" should match "World?" ???? Commented Nov 28, 2011 at 10:04
  • Yes, I want it to ignore the question mark. Commented Nov 28, 2011 at 10:08

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

.contains() is casesensetive, while OP uses equalsIgnoreCase. stackoverflow.com/questions/86780/… might be usefull
You are right. But at the same location, you can fin the method toLowerCase(). But I'll fix my answer.
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.
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.'
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").

Comments

1

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

IndexOf and others

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.