splitting a string

I’m using javascript and I have a string called filetext created something like this …

	var filedown = new WWW(FileURL);
	filetext = filedown.data;

Now what I really need to be able to do is to split the string contained in filetext on the newline character. I have tried

	filetext.split("\n");
	print(filetext[1]);

Which compiles okay but Unity produces an error at runtime telling me it can’t find split.

Any suggestions?

Thanks,
M

Of what type you have declared the ‘filetext’ variable? If you just did ‘var filetext’ (instead of ‘var filetext : string’) then it’s dynamically typed, and tries to find methods at runtime.

WWW.data returns a string, and strings only have Split method with an uppercase ‘S’.

Code like this works:

var filedown = new WWW("http://www.unity3d.com");
var filetext = filedown.data; // implicitly types as string
var lines = filetext.Split( "\n"[0] );
for( var l in lines )
    print( l );

The “\n”[0] takes first character of that string which is somewhat clunky but works.

That works! now i have another problem though :frowning:

I want to split the string at a given position, I’m trying something along the lines of:

for (var l in lines)
{
   a = l.substr(0,3);
   b = l.substr(3,l.length-3);
   /* do some stuff */
}

But it doesn’t like substr!!!

Is there a reference somewhere on the string functions that Unity supports?

There is a huge reference, in fact. (Since Unity uses Mono which is like Microsoft’s .NET)

You want the Substring method. (Have to get used to Mono’s capitalization)

-Jon

p.s. Microsoft’s reference site will make you cry. You might have luck with Mono’s reference: http://www.go-mono.com/docs/index.aspx?link=T%3ASystem.String

Thanks! :smile:

//post deleted//

Silly me, I was getting funny error messages but a close and reopen of Unity fixed the problem.