My game has to use a text file to give it instructions in a specific order, so the files are full of symbols. In my script, I import a TextAsset with Resources.Load, and split that object by newlines into an array with this command:
string[] lines = Regex.Split(theTextObject.text, "\n|\r|\n\r");
Then, I run those lines (with a for loop) through this switch-case statement:
switch(lines[i])
{
case "-":
//Code
break;
case "+":
//Code
break;
case "/":
//Code
break;
case "!":
//Code
break;
default:
Debug.Log("Fell out at line " + lines[i]);
}
This technically works, but the Regex statement doesn't actually seperate it correctly. Between each line, it adds a completely blank string to the array, making a segment of it look like:
{"+", "", "-", "", "/", "", "!", ""}
Instead of
{"+", "-", "/", "!"}
So, I fixed that issue by changing the Regex.Split operator to just "\n", making it look like:
string[] lines = Regex.Split(theTextObject.text, "\n");
That makes it look right, with each line correctly seperating into the array, and no empty strings. The problem is, the switch-case statement no longer works. At all. Each line goes to the default and doesn't get matched against the cases. My log statement confirms that:
Fell out at line +
Fell out at line -
Fell out at line / etc.
Those symbols perfectly match the cases are checking for, but without those empty lines the cases don't work. How do I fix this?