admin管理员组

文章数量:1323722

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?

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?

Share Improve this question asked Jun 24, 2009 at 6:21 timdisneytimdisney 5,36710 gold badges37 silver badges31 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 3

Take a look at the Java docs: http://java.sun./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.

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

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

Rhino provides only the methods that java.lang.String is missing and split obviously isn't one of them.

In order to use JavaScript's implementation of split, you'll have to convert Java string to JavaScript one:

var s = String(new java.lang.String("1 2 3"));
// Also valid: var s = "" + new java.lang.String("1 2 3");
print(s.split()); // 1 2 3

Not exactly the same context, but may help someone.

I use the JavaScript function split() in Rhino.

To get things working, I follow the pattern :

var l_VAR = "" + some_function();
var l_VARs = l_VAR.split("%%");

I suppose that "" + forces Rhino to use a JavaScript type of String.

When I forget to add "" + then I got the message

TypeError: split is not a function.

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

本文标签: javascriptSplit java strings in RhinoStack Overflow