- Home /
How Do I Convert String to String Array In List.Add?
Hello! I'm working with a password system for "Computers" in my game. I want to make it so you can add and save passwords to the system. After a lot of tedious work, I got to a point where I use string.Join to turn my list of strings into one string. I then load my string, where I want to split it and turn it into a list. I will then use that list and set the list of codes. The only issue is I can't split the string in the List.Add function, and I don't believe I can add an array to a list. If anyone could help, here is the code: private const string seperator = "!SEPARATE_VALUES!";
public GameObject disableSelf;
public GameObject loadNextOne;
public GameObject loadNextTen;
// Stored Strings
public List<string> codesLvlOne;
public List<string> codesLvlTwo;
public List<string> codesLvlThree;
public List<string> codesLvlFour;
public List<string> codesLvlFive;
public List<string> codesLvlSix;
public List<string> codesLvlSeven;
public List<string> codesLvlEight;
public List<string> codesLvlNine;
public List<string> codesLvlTen;
//Load Strings
private List<string> codesLOne;
private int i = 0;
void Start () {
string CodesOne = File.ReadAllText(Application.dataPath + "/SaveData/passwordListOne.txt");
string[] splitCodesOne = CodesOne.Split(new[] { seperator }, System.StringSplitOptions.None)
while (i <= splitCodesOne.Length) {
codesLvlOne.Add(splitCodesOne[i]);
i++;
}
}
void OnApplicationQuit () {
SavePasswords();
}
public static void SavePasswords () {
List<string> CodesLOne = string.Join(seperator, codesLvlOne);
File.WriteAllText(Application.dataPath + "/SaveData/passwordListOne.txt", CodesLOne);
}
Well, the only wrong thing I see is you're using "codesLvlOne.AddRange(splitCodesOne); No need to make the loop yourself.
Answer by OGDE01 · Nov 24, 2018 at 03:50 AM
@ecv80 Thanks for the help! I was able to condense my work, even though I figured out I forgot a semicolon! Sorry if I was too vague with the issue, even if you helped anyway. The editor now says:
The best overloaded method match for string.Join(string, string[]) has some invalid arguments
I tried adding the .ToArray() function to the saving part, but it keeps saying:
Please help! ,@ecv80 Thanks so much! I figured out that I was missing a semicolon right above the while statement! I managed to shrink my code using the List.AddRange function! Sorry if I was a bit vague on the issue. Now in the editor, there is a console error saying: "The best overloaded method match for `string.Join(string, string[])' has some invalid arguments". Any idea how to fix this?Argument #2 cannot convert string[] expression to type string
Ah, yeah, I missed that semicolon and all the saving part assu$$anonymous$$g it works already.
This should be good:
public static void SavePasswords () {
string myLongString = string.Join(seperator, codesLvlOne.ToArray());
File.WriteAllText(Application.dataPath + "/SaveData/passwordListOne.txt", myLongString);
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Making lists out of other lists 1 Answer
How to modify array values? 1 Answer