Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Working with string

Community Beginner ,
Jan 20, 2018 Jan 20, 2018

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.

TOPICS
Scripting
1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 20, 2018 Jan 20, 2018

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 20, 2018 Jan 20, 2018

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 20, 2018 Jan 20, 2018
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines