views:

316

answers:

3

I'm trying to split a java string in a Rhino javascript program

var s = new java.lang.String("1 2 3");
s.split();

which give me the error

js: Can't find method java.lang.String.split().

The Rhino docs mentioned that all the javascript String.prototype methods (like match, split, etc.) are available on java string if they're not already provided by java.lang.String. Any ideas on what's going on here?

A: 

it may be that you're using it incorrectly. doesn't split require a string parameter?

Victor
+2  A: 

Take a look at the Java docs: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html

Doesn't seem to be a 0 parameter constructor for the split method. You gotta pass it a regular expression.

Also, for further clarification, the split method returns a string array, it's not a void method like the way you've used it in your sample code.

Sev
+2  A: 

split takes an argument, which is the regular expression you want to use to split your tokens.

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html

Amir Afghani