- Home /
Unity JS Equivalent of split()?
It looks like Unity does not support Javascript's String.split()
(or PHP's explode()
)... Is there an equivalent of this?
How would you convert a delimited string to an array? This would be useful for loading level data stored in an external server.
http://answers.unity3d.com/questions/24539/what-is-the-0-in-string-split explains the syntax for why there is a [0]
Answer by Mike 3 · Dec 25, 2010 at 07:03 AM
You use the .NET one ( http://msdn.microsoft.com/en-us/library/system.string.split.aspx )
yourString.Split(","[0]);
The [0] is a dodgy javascript hack to get the char from the string (as you can't do a char literal)
so, for a string str separated by ;
it'd be str.Split(";"[0]);
? just tried arr=new Array(); arr= str.Split(";"[0]); Debug.Log(arr[0].ToString());
nothing is reading up in Debug.Log(arr[0]);
It returns String[] not Array. if you just do var arr = str.Split(";"[0]); Debug.Log(arr[0]); it should work
it looks like either var arr:Array; arr=new Array();
or var arr:String[];
works - this seems like a temporary hack though, do you know if it'd be deprecated?
If I were you, I would use C# for this exact purpose, greater power and the entire $$anonymous$$ono/.NET library
Oh right - javascript probably just does implicit conversions for native arrays to Array. Shouldn't be deprecated. And, uh, you can use pretty much the whole of .NET with js fireDude o.o
Answer by Chris 21 · Jan 03, 2011 at 11:31 AM
same question, I've yet to find a working solution without giving a format error when attempting to split an array in javascript.
You should put this in a new question so it's easier to deal with - add in the code you're trying and the exact exception it's throwing
ah sure, I suppose this will be moreso a fair warning then. I was using PlayerPrefs to write some Vector3/Quaternion's to file using the ToString() method and made a blind assumption it wouldn't append anything to the written data. rather it writes parenthesis and commas to delineate the components which were giving a nonsensical error in unrelated to the formatting.
anyways the quick solution was:
str = PlayerPrefs.GetString("player.position"); str = Regex.Replace(str,"[(),]",""); splitter = str.Split(" " [0]); PlayerStateHandler.gPlayer.transform.position = Vector3(parseFloat(splitter[0]), parseFloat(splitter[1]),parseFloat(splitter[2]));
Your answer
![](https://koobas.hobune.stream/wayback/20220613092513im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
StringSplitOptions.RemoveEmptyEntries - Unknown identifier 2 Answers
issues splitting strings 1 Answer
Extract number from string? 3 Answers
Split a string every 'n' characters? 5 Answers
How to split a string into array? 2 Answers