Trim String in Nashorn Javascript (js) Engine in Java 8




Program/ Example of Nashorn Javascript (js) Engine in Java 8 - Trim String in javascript >

package nashornJs;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
/**
*
* Trim String in javascript
*
*/
public class MyNashornClass {
   public static void main(String[] args) throws Exception {
      System.out.println("In java - START");
      
      ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
       engine.eval("load('E:/pocJmse/myNashornJavaScript.js')");    
       System.out.println("In java - FINISH");
      
   }
}
/* OUTPUT
In java - START
In myNashornJavaScript.js
1) string trim from both sides
ahi2
2) string trim from Left side
ahi   2
3) string trim from Right side
a   hi2
End myNashornJavaScript.js
In java - FINISH


*/



E:/pocJmse/myNashornJavaScript.js looks like this >
print('In myNashornJavaScript.js');
// 1) string trim from both sides
print('1) string trim from both sides');
print(("a"+"   hi   ".trim() +"2"));
// 2) string trim from Left side
print('2) string trim from Left side');
print(("a"+"   hi   ".trimLeft() +"2"));
// 3) string trim from Right side
print('3) string trim from Right side');
print(("a"+"   hi   ".trimRight() +"2"));
print('End myNashornJavaScript.js');




Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.

eEdit
Must read for you :