- Home /
String.Split() Function Issues
Hi, all! I am saving data currently with PlayerPrefs for my web game. There is a good amount of data, and 3 save slots. My short solution to this is taking all variables that need to be saved, compiling them into a string, and inserting a Pipe character (" | ") Between the variables. Then I go to split the string when I load it using this:
function LoadSlot(){
//Not actual loaded variable. Made-up one for testing purposes
var LoadStr = ("KHizzle|color.white|4252|010110|2434|123456789012345678901234567890123456789012");
//var LoadStr = PlayerPrefs.GetString("Slot"+CurrentSlot);
// The above line is the real code to load it, but since I am debugging it, I supply the long string myself.
LoadStr.Split("|"[0]);
Name = LoadStr[0].ToString();
Debug.Log("Loaded Name as " + Name);
}
My problem here is that when I run this code, I get an
IndexOutOfRangeException: Array index is out of range.
Exception. And the Debug, when I insert a '1' instead of a '0' gives me this:
Loaded Name as H
Which should not be. It should be separating AT the pipe sign and not after every character, right? LoadStr[1] should be: color.white
How do I fix this issue? Thanks!- YA
Answer by SolidSnake · Aug 17, 2012 at 04:19 PM
LoadStr is a string variable you need an array to store the values returned from Split function.
String[] array = LoadStr.Split("|");
==Parsing example==
// in JS
var myLongValue: long = long.Parse(myValueInString);
var myColor: Color = new Color(float.Parse(myRedValueInString),float.Parse(myGreenValueInString),float.Parse(myBlueValueInString));
Thank you so much! Lastly- I know I can get something ToString();, but what if I need to convert it to a Color, or an Int?
.net 2.0 API allows you to do this.. have a look at the int.Parse() and int.TryParse() (or in your case you might need to use long ins$$anonymous$$d of int(Int64))
for the color you will need to store the RGB values and then convert them to color using the Color Class.. I will update my answer with example
Now I just need to create a for loop to split up this: "1.0000.0000.0001.000" into 4 different variables. I can do that with substring right? Doesn't the second value deter$$anonymous$$e the length of the returned substring?
are these the values of the rgba? did you store them in this format (i.e. separated by dot) or it was given to you this way?
if you are storing them yourself, it will be easier if you store them separated by a different character than a dot e.g. an underscore '_' so you can use split ins$$anonymous$$d. Substring can be used if you always have the same length of your variables and yes when you use substring then the second value deter$$anonymous$$e the length of the substring
Your answer
Follow this Question
Related Questions
Multiple Animations for the Same GameObject 2 Answers
character lagging in air when jumped 0 Answers
Why Am I getting MissingMethodException message? 3 Answers
Talking Code 2 Answers
Not getting a Debug.Log 2 Answers