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("

"[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

If you want the result to be an array you have to use arrays… :smiley:

var playerEntries : String[];
var playerData : String[];
var playerNamePull : String[];  // They are arrays now !
var playerScorePull : int[];    //         ||

function Start()
{
    // Read in formText
    playerEntries = formText.Split("

"[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*.Split("-"[0]);*

playerNamePull = playerData[0];
playerScorePull = System.Convert.ToInt32(playerData[1]);
}
}

Remove the [0] from inside the Split function