- Home /
Splitting String Into Array
Hello,
I have string variable that holds a load of text, and I am trying to split it.
The string is basically player names, and player scores, it is displayed like this:
"player-score player-score player-score" --> "oliver-10 alex-20 tom-45"
And I want to break it out into arrays.
This is what I have so far (Start Function):
var playerEntries : String[];
var playerData : String[];
var playerNamePull : String;
var playerScorePull : int;
playerEntries = formText.Split("\n"[0]);
for(var entry : String in playerEntries){
playerData = entry.Split("-"[0]);
playerNamePull = playerData[0];
playerScorePull = System.Convert.ToInt32(playerData[1]);
}
Now, the playerEntries array works fine:
oliver-10
alex-20
tom-45
But nothing in the 'for' works - what am I doing wrong?
I'm trying to get it like this:
playerNamePull (string array) :
oliver
alex
tom
playerScorePull (int array) :
10
20
45
Thanks
Answer by Bunny83 · Jun 13, 2011 at 02:51 AM
If you want the result to be an array you have to use arrays... :D
var playerEntries : String[];
var playerData : String[];
var playerNamePull : String[]; // They are arrays now !
var playerScorePull : int[]; // ||
function Start()
{
// Read in formText
playerEntries = formText.Split("\n"[0]);
playerNamePull = new String[playerEntries.Length]; // Create the arrays with the
playerScorePull = new int[playerEntries.Length]; // same size as the playerEntries array
// To work with multiple arrays we need an index, so I changed the for (each) into a "normal" for loop
for(var i = 0; i < playerEntries.Length; i++){
playerData = playerEntries[i].Split("-"[0]);
playerNamePull[i] = playerData[0];
playerScorePull[i] = System.Convert.ToInt32(playerData[1]);
}
}
Thank you so much 4 year old answer! you helped prevent me from duplicating this question for an array I needed to split into 7 more arrays :D I wish I could give you a cookie. Now I just need to apply the filter option to remove the blank element at the end of the first array haha.
Answer by Oliver Eberlei · Jun 12, 2011 at 10:28 PM
Remove the [0] from inside the Split function
Now I get: No appropriate version of 'String.Split' for the argument list '(String)' was found
Hm, I just copy and pasted your code and it works fine. Does your PHP script deliver the right input?
This is the code i used to test
var playerEntries : String[];
var playerData : String[];
var playerNamePull : String;
var playerScorePull : int;
function Update() { var formText = "oliver-10\nalex-20\ntom-45";
playerEntries = formText.Split("\n"[0]);
for(var entry : String in playerEntries){
playerData = entry.Split("-"[0]);
playerNamePull = playerData[0];
playerScorePull = System.Convert.ToInt32(playerData[1]);
Debug.Log( playerNamePull + ": " + playerScorePull );
}
}
$$anonymous$$y PHP works fine: I have just noticed that in my playerEntries array, I get 3 extra elements that are empty. I also want playerNamePull, and playerScorePull to be arrays if possible.
playerNamePull, and playerScorePull in the inspector appear to have nothing in it - although, it prints the names and scores
Your answer
![](https://koobas.hobune.stream/wayback/20220612103704im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to convert a string to int array in Unity C# 1 Answer
Change part of a string [Solved] 3 Answers
Strings, Arrays and Split in js 1 Answer
Split String into Arrays 1 Answer