How do i get array index that contains spesific string
Why do I keep getting -1 as Array index? Here is my code
public string[] data;
foreach (string x in data) {
int index = System.Array.IndexOf (data, x.Contains ("Verant-" + this.transform.parent.name));
Debug.Log (index.ToString ());
}
Comment
Best Answer
Answer by NoseKills · Jun 30, 2016 at 03:26 PM
You might see clearer what is happening if you wrote each method call on separate row.
bool doesXContainString = x.Contains ("Verant-" + this.transform.parent.name);
int index = System.Array.IndexOf (data, doesXContainString);
All i did there is take the x.Contains() call out of the parenthesis and wrote it open.
I guess you wanted to search for any index where the string contains "Verant-" +name. Instead you search for a boolean in the array of strings.
The way to do this would be (for example)
public string[] data;
int index = 0;
for (; index < data.Length; index++) {
if (data[index].Contains ("Verant-" + this.transform.parent.name))
{
break;
}
}
Debug.Log (index.ToString ());
Your answer
Follow this Question
Related Questions
[help] Array index is out of range 0 Answers
C# Array Of Class Read Variables 2 Answers
C# If string is equal to any in array 2 Answers
Check if a object with a specific tag exists in an array 1 Answer