admin管理员组文章数量:1323202
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 badges5 Answers
Reset to default 3Take 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
版权声明:本文标题:javascript - Split java strings in Rhino - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742123374a2421826.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论