Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to use ScriptEngine of Java but I have some trouble with javascript split function like below:

    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("javascript");
    Bindings bindings = engine.createBindings();
    bindings.put("example", "123/456/789");

    String s1 = "var obj = example.split(\"/\"); print(obj[0]);";

    Object result = engine.eval(s1, bindings);

    System.out.println(result);

when I evaluate the script; this code prints "123null" because result is null. When I tried return statement instead of print like this:

    String s1 = "var obj = example.split(\"/\"); return obj[0];";

throws an exception:

Exception in thread "main" javax.script.ScriptException: sun.org.mozilla.javascript.internal.EvaluatorException: invalid return (#1) in at line number 1

So how should I get the value of first index from this splitted native array?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Try:

var obj = example.split(\"/\"); obj[0];
share|improve this answer
    
thank you for your help, this worked for me. –  cgrgcn May 31 '13 at 10:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.