These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

strange result with substr

JGarn23
Registered: Feb 17 2010
Posts: 35
Answered

I've got a simple script that pulls the first two chars from a string (as part of a date calculation).
Substr seems to work fine on any string starting with anything other than 08 or 09, but returns zero when the string starts with 08 or 09.

var res;
var str;

str = '07';
res = parseInt(str.substr(0,2));
xfa.host.messageBox("Result is: " + res); // this prints '07'

str = '08';
res = parseInt(str.substr(0,2));
xfa.host.messageBox("Result is: " + res); // this prints '0'

Any ideas?
Thanks.

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
I believe 8 is not a valid number in the octal based number system.

If you want to pull numeric character strings as characters and not number, I would only use the 'substr' method and not use the 'parseInt' method. 'it' assumes the output is a number. I would also provide the radix parameter so the default of the Octal number base is not assumed when a value starts with '0' or no radix is provided. You can also use the 'typeof' operator to test for or see what the type of data you are dealing with.

You could try using the strings ,08', '09','10', an '11' and observe what happens and try to figure out why some numbers work but others do not.

George Kaiser

JGarn23
Registered: Feb 17 2010
Posts: 35
Brilliant! Many thanks. It didn't cross my mind it was anything to do with the number base!