- Home /
For loop & Foreach don't complete inside IEnumerator when using IndexOf
Hi, I'm having a weird problem where a loop won't complete when I use IndexOf inside the loop.
Here's my code:
for (int i = 0; i <= games.Length; i++)
{
if (games[i] != "")
{
iGUIButton button = scrollView2.addElement<iGUIButton>();
button.type = iGUIButtonType.ButtonBigYellow;
button.label.text = games[i].Substring(0,games[i].IndexOf('?'));
button.variableName = games[i].Substring(games[i].IndexOf('?') + 1, games[i].Length);
button.clickCallback += play;
}
}
Thanks!
What does "not complete" mean? An infinite loop? in other words a crash? Does it ter$$anonymous$$ate too early? Do you actually talk about the loop or about the coroutine? Do you get any errors? If so you might want to post them here.
btw your code will produce an array-out-of-bounds error because the largest possible index into the "games" array is (games.Length-1) but you iterate until (games.Length)
Hi, Sorry, I forgot to mention that all values do contain a '?'. By not complete I mean that it runs once, some lines of the code don't execute at all. It runs perfectly if I remove the IndexOf and just use the strings. Edit : Also, I'm calling the StartCoroutine in void Start()
foreach (string game in games) { //code } does the same thing with the IndexOf in the code. Runs find when not doing any IndexOf operations.
I think the problem is caused by index, with indexOf("?") + 1 you could go out of the string.. are you sure the "?" cant be found at the end of the string? Anyway try to edit this: button.variableName = games[i].Substring(games[i].IndexOf('?') + 1, games[i].Length); to button.variableName = games[i].Substring(games[i].IndexOf('?') + 1, games[i].Length - 1);
By design, there are always exactly 9 characters after the "?" so that's not the issue. I'm afraid
button.variableName = games[i].Substring(games[i].IndexOf('?') + 1, games[i].Length - 1);
didn't help either. So the string I recieve is :
test2?testes562,test1?testtes102
yet,
string[] games = swww.text.Split (','); foreach (string game in games) { if (game != "") { Debug.Log(game.Substring(0,game.IndexOf('?'))); Debug.Log(game.Substring(game.IndexOf('?') + 1, game.Length)); }
Would only return
test2
Answer by ReVo_ · Oct 20, 2013 at 05:35 PM
EDIT3: ok i think i found the problem
If we have this string:
Hello World
If we start to extract from the o
Hell"o" (the o)
(Code:
string hello = "Hello World";
Console.WriteLine(hello.Substring(hello.IndexOf('o'), hello.Length));
)
and then we set length to hello.Length it except to extract hello.Length chars from startIndex index (hello.indexOf)
i think this can fix the problem, try:
string.Length - indexOf('?')
I update this too in case someone have the same problem and dont see comments
EDIT2: Since in the comment it look horrible:
Edit
button.variableName = games[i].Substring(games[i].IndexOf('?') + 1, games[i].Length);
to
button.variableName = games[i].Substring(games[i].IndexOf('?') + 1, games[i].Length - 1);
Anyway i think indexOf('?') + 1 can be a problem, are you sure the '?' cant be found at the end of the string? If the ? is at the end it can go out of the string
EDIT: As bunny83 said, the
for (int i = 0; i <= games.Length; i++)
is wrong because "i" will be games.Length and it will cause a index-out-of-bounds exception.
indexOf returns -1 if it failed the search (the string dont have the char).. maybe its the problem?
(I cant comment)
Hi, Thanks for answering, please refer to my comment on the question.
Answer by rutter · Oct 20, 2013 at 07:30 PM
As has been pointed out twice, now, your for loop is likely to cause an IndexOutOfRangeException.
In addition, your substring call:
games[i].Substring(games[i].IndexOf('?') + 1, games[i].Length);
Is likely to cause an ArgumentOutOfRangeException. When calling string.Substring(), you need to make sure that startIndex
plus length
indicates a valid index in the string.