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

Javascript equivalent to VB's LEFT()

Gheezer
Registered: Feb 16 2009
Posts: 19
Answered

In VB's Left("text", 3) show the left 3 characters of the text. Does Javascript have anything equivalent?

Troy

My Product Information:
Acrobat Pro 8.1.1, Windows
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
In JavaScript there are the "substr(fStart, fLength" and "substring(fIndexA, fIndexB)" methods available for text strings. Since this could be a repeating operation, you might consider creating a document level function like:

function Left(sString, fLength) {// returns the left most flength characters of the sStringreturn sString.substr(0, fLength);}

Example:
var sMyString = "AbCdEfGh";var fChars = 3;var sLeft3 = Left(sMyString, fChars);console.show();console.clear();console.println("Left " + fChars + " characaters of  \"" + sMyString + "\"  are: " + sLeft3);

Produces:
Quote:
Left 3 characaters of "AbCdEfGh" are: AbC

George Kaiser

Gheezer
Registered: Feb 16 2009
Posts: 19
gkaiseril wrote:
In JavaScript there are the "substr(fStart, fLength" and "substring(fIndexA, fIndexB)" methods available for text strings. Since this could be a repeating operation, you might consider creating a document level function like:
function Left(sString, fLength) {// returns the left most flength characters of the sStringreturn sString.substr(0, fLength);}

Example:
var sMyString = "AbCdEfGh";var fChars = 3;var sLeft3 = Left(sMyString, fChars);console.show();console.clear();console.println("Left " + fChars + " characaters of  \"" + sMyString + "\"  are: " + sLeft3);

Produces:
Quote:
Left 3 characaters of "AbCdEfGh" are: AbC
Not as straightforward as VB but it will do. I'll test it out later.

Thanks!

Troy