0

I have an array of type string that contains a bunch of lines of text that are broken up in the different elements of the array. I want to check which element contains the string Name: in it. The reason I need to do it like this is because Name can be in a different element when ran on different arrays. I cant use .contains on an array so I am not sure what to use.

1
  • think about what String.contains() does and how it relates to the structure of your array. Commented Oct 24, 2012 at 19:25

2 Answers 2

2
String []array = new String[]{"foo","bar","the_string_you_are_looking_for"};

int index = -1;
for (int i = 0; i < array.length; ++i)
{
   if (array[i].contains("the_string_you_are_looking_for"))
   {
       index = i;
       break;
   }
}
Sign up to request clarification or add additional context in comments.

3 Comments

@Heisenbug array.size(): array has no method size(). Should be array.length :)
oh thanks, that's true. I'm switching between several languages these days..my fault
@Heisenbug It already is confusing enough with the different versions in Java to get the length of an container: array=length, String=length(), Collection=size()
2
int getIndexOfElementContainingName(String[] myStringArray) {
    for(int i=0; i<myStringArray.length; i++) {
        if(myStringArray[i].contains("Name:"))
            return i;
    }
    return -1;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.