views:

8078

answers:

5

In Java is there a way to check the condition:

"Does this single character appear at all in string x"

without using a loop?

Thank you,

+16  A: 

String.indexOf()

mP
But there's always a loop behind that call because you can't find a symbol otherwise.
vava
indexOf() uses a loop internally.
Simucal
Thats not what Barfoon asked. B wishes to avoid doing the loop in B's code. Naturally the API needs to do a loop after all a String is an array of characters wrapped up in a nice class with lots of useful methods.
mP
+9  A: 
  • String.contains() which checks if the string contains a specified sequence of char values
  • String.indexOf() which returns the index within the string of the first occurence of the specified character or substring (there are 4 variations of this method)
Zach Scrivena
a char isnt a CharSequence so it cant be passed to String.contains(CharSequence).
mP
uh... it is trivial to turn a char to a CharSequence, so I don't get the down voting .. + 1
hhafez
The OP doesn't say a char, he/she says a "single character" - which could be a String; String implements the CharSequence interface. +1 for better written and more complete answer.
Nick Pierpoint
+1  A: 

To check if something does not exist in a string, you at least need to look at each character in a string. So even if you don't explicitly use a loop, it'll have the same efficiency. That being said, you can try using str.contains(""+char).

mweiss
Agreed. At some point, somebody, somewhere needs to construct a loop to do this. Fortunately the Java API does this or our code would be very cluttered!
Fortyrunner
+2  A: 

Yes, using the indexOf() method on the string class. See the API documentation for this method

Mystic
+2  A: 

I'm not sure what the original poster is asking exactly. Since indexOf(...) and contains(...) both probably use loops internally, perhaps he's looking to see if this is possible at all without a loop? I can think of two ways off hand, one would of course be recurrsion:

public boolean containsChar(String s, char search) {
    if (s.length() == 0)
        return false;
    else
        return s.charAt(0) == search || containsChar(s.substring(1), search);
}

The other is far less elegant, but completeness...:

/**
 * Works for strings of up to 5 characters
 */
public boolean containsChar(String s, char search) {
    if (s.length() > 5) throw IllegalArgumentException();

    try {
        if (s.charAt(0) == search) return true;
        if (s.charAt(1) == search) return true;
        if (s.charAt(2) == search) return true;
        if (s.charAt(3) == search) return true;
        if (s.charAt(4) == search) return true;
    } catch (IndexOutOfBoundsException e) {
        // this should never happen...
        return false;
    }
    return false;
}

The number of lines grow as you need to support longer and longer strings of course. But there are no loops/recurrsions at all. You can even remove the length check if you're concerned that that length() uses a loop.

Jack Leow
If you define recursion as a non-loop procedure, you're a geek :D +1 for being creative.
furtelwart