- Home /
StringSplitOptions.RemoveEmptyEntries - Unknown identifier
Hello, I am using a String to save scores for my levels (PlayerPrefs). As PlayerPrefs doesn't work with arrays, I just convert all of these into one String to this form: "LVL1score;LVL2score;LVL3score;" and so on.
However, when I try loading from it and spliting it with this code:
prefText = PlayerPrefs.GetString("Levels");
var lvlTexts = prefText.Split(";"[0], StringSplitOptions.RemoveEmptyEntries);
var j : int = 0;
for (lvlInfo in lvlTexts) {
levels[j].levelUnlocked = (lvlInfo[0] == "Y") ? true : false;
levels[j].levelFinished = (lvlInfo[1] == "Y") ? true : false;
levels[j].levelScore = int.Parse(lvlInfo.Substring(2, lvlInfo.length - 2));
j++;
}
i get an error "Unknown identifier 'StringSplitOptions' (i need it to not include "" String after last semicolon). I have tried using import System in the beginning of script, i have tried using System.StringSplitOptions.RemoveEmptyEntries, but nothing work. I have decided to just remove last character in String, which works, but I would still like to understand, why the code above doesn't work. Thanks in advance.
This was asked some time ago at http://forum.unity3d.com/threads/mono-c-string-split-question.17348/ too. And it's not listed in the $$anonymous$$ono site under System, so maybe they just didn't incorporate it?
Which is odd, because it's on the GitHub repo...
Answer by Baste · Jan 07, 2015 at 06:34 PM
Unity works with a (very, very) old version of Mono, for somewhat esoteric reasons (you can search around if you are curious). This means, among other things, that some methods that's available in modern versions of C# - either .NET or modern Mono - are not available in Unity.
My guess is that StringSplitOptions.RemoveEmptyEntries is one of those. Which means you have to do this the old fashioned way: either ignoring empty strings in your loop, or removing them beforehand.
Sure that is true, some newer methods is not available in Unity, but System.StringSplitOptions is not one of them. It is available. I have used it a lot when parsing text. The problem is actually that System.StringSplitOptions does not work with the specific version of the .Split function OP is trying to use.
Answer by GameVortex · Jan 08, 2015 at 08:55 AM
The .Split function you are using does not take StringSplitOptions as a parameter. So without System. in front you get the regular Unknown Identifier, but with it you would get Invalid Arguments.
When you supply the char to split with directly then you are using the .Split function which only takes an optional number of char values to split with.
To use the .Split function which can take the StringSplitOptions you have to supply your char values as an array:
string stringToSplit = "";
char[] splitters = { ';' };
string[] splittedString = stringToSplit.Split(splitters, System.StringSplitOptions.RemoveEmptyEntries);
Awesome. I was so close. But I gave up, then decided to search. Thanks, GameVortex
Your answer
Follow this Question
Related Questions
Unity JS Equivalent of split()? 2 Answers
How to split String? 1 Answer
Skip empty strings after split 3 Answers
What do you say the best way to store a string be? 2 Answers
Trying to parse Facebook FQL response with String.Split in webplayer; no luck 1 Answer