How to split a string into array?

I try using the .Split function like this:

var text = "this is a test bla bla bla this this this";
var textSplit;

function Start () {
textSplit = text.Split(" "[0]);

If I do this Unity acts as I suspect it would by returning “test”.


If I run this or anything higher I get the error message
IndexOutOfRangeException: index


Tells me 4

I can’t find what the [0] is needed for.
If I put anything else then 0 in there I get the error:
IndexOutOfRangeException: Array index is out of range.

The following did return errors:

textSplit = text.split(" ");
textSplit = text.Split(" ",[0]);
textSplit = text.Split(" ");

Is there another function or am I doing something wrong here?

Thanks for your help.

Define textSplit as a string array:

#pragma strict

var text = "this is a test bla bla bla this this this";
var textSplit : String[];

    function Start () {
    	textSplit = text.Split(" "[0]);
    	Debug.Log (text); //this is a test bla bla bla this this this
    	Debug.Log (textSplit); //System.String[]
    	for(var i : int = 0; i < textSplit.Length; i++){
    		Debug.Log(textSplit*); //each split*
  • }*
  • Debug.Log (textSplit[1]); //is*
  • Debug.Log (textSplit[3]); //test*


var str = “A string with many words”;
var strArr:string;

function Start(){
   strArr= str.Split(" ");

I guess that should do it.