Reputation: 43778
I have tried to split a pipe from a string in Mirth connect JavaScrip, but for some reason, it's not working as expecting.
Example:
var x = "RO|123|test|account|test2";
var arr = x.split('|');
I expecting the output when I loop through the variable arr as below:
arr[0] -> RO
arr[1] -> 123
arr[2] -> test
arr[3] -> account
arr[4] -> test2
BUT for some reason the output is as below:
arr[0] -> R
arr[1] -> O
arr[2] -> |
arr[3] -> 1
arr[4] -> 2
Does anyone know why and how could I solve this issue?
Upvotes: 1
Views: 1937
Reputation: 1719
In your actual code, x is probably a Java string, and not a Javascript string like in your example. The Java String.split method takes a regex string as the first parameter.
For this declaration:
var x = new java.lang.String("RO|123|test|account|test2");
Either of these should give the expected result:
// Calling the Java String.split method.
var arr = x.split('\\|'); // arr will be a Java array
// Explicitly convert to a Javascript string to ensure calling
// Javascript String.prototype.split function.
var arr = String(x).split('|'); // arr will be a Javascript array
Note: For those that only picked up on the javascript tag, mirth javascript runs in a Mozilla Rhino environment.
Upvotes: 5
Reputation: 519
It should work fine as it is working as expected.
var x = "RO|123|test|account|test2";
var arr = x.split('|');
/* document.write(arr); */
for(var i =0; i < arr.length; i++){
document.write("arr["+[i]+"] ->"+arr[i]+ "<br>");
}
Upvotes: 0