- Home /
split() for separators more than 1 characters
Is it possible to do a split() in Unity JS to turn a string to array, where the separator has more than one character, as in the below:
Apple<br /> Banana<br /> Canapple<br />
this is related to my previous question, but the solution there only works for single character separators http://answers.unity3d.com/questions/33317/unity-js-equivalent-of-split
Answer by Eric5h5 · Apr 30, 2011 at 06:53 AM
The link that Mike provided in that other question shows the different options for String.Split. As you can see there, there's an option to use a string array instead of a char array, so you can provide a string array with one entry:
var test = "1a11abc2b22abc3c33";
var strings = test.Split(["abc"], System.StringSplitOptions.None);
for (s in strings) print (s);
There isn't a default for StringSplitOptions when you use a string array, so you have to provide something, such as None.
why in the world is it possible to use .net function in a javascript document? http://msdn.microsoft.com/en-us/library/system.string.split.aspx or am i wrong? this is currently turning my whole world upside down.
@Cyborg: Unity uses .NET for all script functionality. (Well, technically it's $$anonymous$$ono.) The language is irrelevant. "Javascript" in Unity is largely similar to JScript.NET
anyway, it's not like web Javascript.
You're going to enjoy your new upside down world. Just about anything you can find in the .Net documentation can be used in Unity's javascript. It's like cool and stuff...
Wow... This answer is pretty awesome and is turning my world upside down. I wish I found it two days ago before I wrote my own string splitter (not that hard, really).
I was trying to use this function yesterday but left out the System in the Split Options and it didn't work so I gave up(I'm a sap, huh, lol). Glad I saw this. I would also recommend to anyone using this for web data to consider tying together complex strings with WWW.EscapeURL(). That way only a single char is needed to split.
Answer by GlennHeckman · Apr 30, 2011 at 06:05 AM
Have you tried this:
var arr = str.Split("<br />"[0]);
Debug.Log(arr[0]);
*Note, I'm pretty sure this will only work if the string actually contains the "<br />
" characters.
That's a single char. "<br />"[0]
will return " $(function () { setupComments($('#answer-container-61678'), true, false, croles, "full", true) }); $(document).ready(function () { var $answer = $("#answer-container-61678"); var $replyLink = $answer.find(".reply-link"); $replyLink.click(function (e) { e.preventDefault(); $answer.find(".add-comment-link").click(); }); });