is it String or string...where is the split function?

Hello guys,

I need to split a string depending on a separator I put into a string?
in Javascript the type ‘string’ would have what I need but the Unity3D apparently doesn’t have a ‘string’ but ‘String’ which seems pretty different… I checked online and there are just 2 methods under the class ‘String’…so… anyone can address me to the proper page?

GC.

Hey man,

Check out http://www.go-mono.com/docs/index.aspx?link=T%3ASystem.String%2F*
It should have every method you can use.

Thank you but I keep having an error from Unity… saying that there is no appropriate version of

var prms = param.Split(“+”, 2);

What are you trying to do with the “2” in there? Is that the count?

Try:
var prms = param.Split(‘+’, 2); //uses apostrophes around the + sign instead of quotes

or

var prms = param.Split(“+”[0], 2);

Thanks Fiz,

this is my code: var prms = param.Split(‘+’, 2);

In real javascript it would be Split with small S. The int is the count yes. I can’t find anything about the strings on the Reference Manual Online… and I just want a string split in 2 parts and the separator is ‘+’. I get from unity:

var prms = param.Split(‘+’, 2); //Assets/Login/wwwManager_script.js(164,32): BCE0044: unexpected char: ‘’'.
or
var prms = param.Split(“+”,2);
Assets/Login/wwwManager_script.js(165,31): BCE0023: No appropriate version of ‘String.Split’ for the argument list ‘(String, int)’ was found.

following the go-mono manual there should be any of those options.

If you’re using javascript/unityscript, it’s done this way:

var sentence = "The quick brown fox jumped over the lazy dog";
var words = sentence.Split(" "[0]);
Debug.Log(Array(words));

what’s that [0] mean?

I believe it’s simply converting the string to a char. For some reason I don’t quite fathom, this is required for unityscript.

You’d probably want something along these lines:

var prms = params.Split("+"[0]);

Note that Split() returns a String array.

Thank you tonyd,

I dunno if Split returns or not an array, to be honest I don’t think so as if I debug.log(Array(prms)) where

var prms = params.Split(“+”[0]);
I get: element1 , element2 :frowning: and if I pring prms[0] I just get the first element and not the second one :frowning:

Microsoft knows:

http://msdn.microsoft.com/en-us/library/system.string.split.aspx

may be Microsoft knows… but
still if I debug prms[0] I get the first element, but the second element is empty :frowning:

The second element is prms[1]

Just try:

for (item in prms) Debug.Log(item);

Go to the console and the contents of the array will be displayed.

yup, I guess I’ve found the bug… everything makes sense now, although is pretty different from the string in standard javascript.

thanks everybody for your help.

the [0] notation on a string returns the char at that location in the string. IIRC, JavaScript has no concept of characters, only strings. And silly me, of course the apostrophe syntax in C# to designate a character doesn’t work in JavaScript because of that (you can use either-or to denote a string) I guess there’s no easy way to define a char, eh? ;_;

You could encapsulate that into a function to make it slightly simpler

function Split(sString, sSplit, nCount){
return sString.Split(sSplit[0], nCount);
}