Take the 2-minute tour ×

This question already has an answer here:

I Want to be able to find a certain word with in a string. Here is an example of code

    String names[];

    names[] = "bob, frank, mark, gary";

    if(names[] contains "bob"){ //do code}

This is obviously not how the code works but it gives you the idea of what I want to achieve.

share|improve this question
add comment

marked as duplicate by Tim Cooper, Geobits, RiaD, nickb, ahmet alp balkan Jul 30 '13 at 0:51


3 Answers

You can either search for it yourself like this:

boolean contains = false;

    if (names[i].equals("bob"))
         contains = true;

Or you can use a function

boolean contains = Arrays.asList(names).contains("bob");
share|improve this answer
add comment

You can also use the .toString method of the Arrays util class:

String[] names = new String[] { "Alice", "Bob", "Cloud Strife" };



This only works on string arrays :)

share|improve this answer
add comment
for(String name : names){
  if (name.contains("bob")){
share|improve this answer
add comment
