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.
2 Answers
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;
}
}
3 Comments
halex
@Heisenbug
array.size(): array has no method size(). Should be array.length :)Heisenbug
oh thanks, that's true. I'm switching between several languages these days..my fault
halex
@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()