Working with string
Copy link to clipboard
Copied
Version: Indesign CS 5.5, ExtendScript Toolkit CS5.5
var aaa=["4+5",6,7]; //my array
alert(aaa[0].length); //get length of aaa[0]
var x =aaa[0].substring(0,1); //get char
var y =aaa[0].substring(1,1);
var z =aaa[0].substring(2,1);
alert(x);
alert(y);
alert(z);
i think result of x ="4",y=", " and z="5" but not right. Mycomputer alert x="4", z="+".Plese help me explain this.
Copy link to clipboard
Copied
With the substring method if the argument’s first number is greater than the second they get reversed, so (2,1) == (1,2)
There is an InDesign scripting forum. You’ll get better answers for coding questions there
Copy link to clipboard
Copied
Hi,
you mixed up method substr() with substring().
var s = "4+5";
s.substr(0,1) // 4
s.substr(1,1) // +
s.substr(2,1) // 5
Adobe InDesign CS6 (8.0) Object Model JS: String
Regards,
Uwe
Copy link to clipboard
Copied
Hi sangn33723990​
Better use charAT() in such cases
var x =aaa[0].charAt(0); //get char
var y =aaa[0].charAt(1);
var z =aaa[0].charAt(2);
or like Laubender​ mentioned use substr()
or like this
var x =aaa[0].substring(0,1); //get char
var y =aaa[0].substring(1,2);
var z =aaa[0].substring(2,3);
Aktualisiert
explanation
String.substring(indexFirstChar[, indexLastChar]*); // ]* = optional, if not used --> till the end
String.substr(indexFirstChar[, lenght]*); // ]* = optional, if not used --> till the end

