- Home /
String.Split problem
As far as I understand it, the String.Split method will break up a string based on a specified separator and enter these sub-strings as elements of an array.
For the sake of understanding this (and eventually using it) I've assembled a simple version of this to see how it works, but when I try to run it I get an "IndexOutofRangeException: Array index is out of range". I assume this means that it runs out of array index points to enter the sub-strings into, but I don't understand how that could happen, because I'm using a dynamic and not built-in array.
Here's my code:
var highestPriority = "1,2,3";
var priorityCache = highestPriority.Split("," [3]);
I've also tried doing this and explicitly declaring the variable types outside of the function, but that doesn't seem to have any effect.
Thanks for any help you can provide.
Answer by AnaRhisT · Jun 30, 2010 at 10:45 PM
Solution:
var highestPriority = "1,2,3";
var priorityCache = highestPriority.Split(","[0]);
for(i = 0; i < priorityCache.Length; i++){
print(priorityCache[i]);
}
yeah, this solves it. Somewhere I read that the parameter after the separator was a count of the total substrings, and not specifying the first character to start from
It's not another parameter - it's grabbing the first character from the string. It's a dodgy hack to get around js not being able to declare char literals
Where is this documented in Unity? I can't find anything about it, and I know it's not standard Javascript.