Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-21-2013, 09:00 PM   PM User | #1
Bent2BitsŪ
New to the CF scene

 
Join Date: Feb 2013
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
Bent2BitsŪ is an unknown quantity at this point
How to extract a letter from a string

Hi all,

I am trying to create a dynamic stamp in Adobe which requires me to write Javascript for a textbox control. The stamp has two text boxes that I need to fill from one string. The string is in the format of
"08363-G-5109-DWG-R-00001_A.pdf" with a varying number of characters before the underscore, but always the same after the underscore. In the first text box I need
08363-G-5109-DWG-R-00001 which I get with
Code:
event.value = event.source.source.documentFileName.slice(0,-6);
this works great. The second box needs to contain the letter after the underscore, in this case 'A' The code that I have found that relates to getting this value doesn't seem to work. I am a VBA programmer I suspect that the logic is dfferent in Javascript.

Code:
var str = event.source.source.documentfilename;
var strt = str.lastindexof("_");
var end = str.lastindexof(".");
event.value = str.substring(strt,end);
Any assistance would be greatly appreciated!
Bent
Bent2BitsŪ is offline   Reply With Quote
Old 02-21-2013, 10:20 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Code:
var str = event.source.source.documentfilename;
var strt = str.lastindexof("_");
event.value = str.charAt( strt + 1); // if always a single character
Note that JS supports both substring and substr:

str.substring( start, end ) -- gets character starting at start through end MINUS 1.
"abcdef".substring( 2,4 ) would be "cd"

str.substr( start, length ) -- get characters starting at start for given length
"abcdef".substring( 2, 3 ) would be "cde"

character positions start at zero, not 1 as in VB derivatives.

In both of those, if you omit the second argument, you get from start to the end of the string.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 02-21-2013, 10:22 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Looks like your code would have worked if you had used
Code:
event.value = str.substring(strt+1,end);
But again, if you only needed the one character, charAt() is easier.

Or you could have used
Code:
event.value = str.substr(strt+1,1);
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
Bent2BitsŪ (02-21-2013)
Old 02-21-2013, 10:30 PM   PM User | #4
007julien
Regular Coder

 
Join Date: May 2012
Location: France
Posts: 115
Thanks: 0
Thanked 17 Times in 15 Posts
007julien is an unknown quantity at this point
A variant consists to split your string with the underscore

var arrStr="08363-G-5109-DWG-R-00001_A.pdf".split('_');

alert('Before:' +arrStr[0]+', after:'+arrStr[1]);
007julien is offline   Reply With Quote
Users who have thanked 007julien for this post:
Bent2BitsŪ (02-21-2013)
Old 02-21-2013, 11:42 PM   PM User | #5
Bent2BitsŪ
New to the CF scene

 
Join Date: Feb 2013
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
Bent2BitsŪ is an unknown quantity at this point
Hi folks,

Thank you for your input on this, I almost have it working. I tried all the solutions above and the closest I could get was with:

Code:
var arrStr="08363-G-5109-DWG-R-00001_A.pdf".split('_');
var arrstr2 = arrStr[1].split(".");
event.value = arrstr2[0];
which gave me my desired result 'A', but requires that the string be inserted in the javascript each with each document opened.

Code:
var arrStr = event.source.source.documentfilename.split("_");
var arrstr2 = arrStr[1].split(".");
event.value = arrstr2[0];

and 

var str = event.source.source.documentfilename;
var arrStr = str.split("_");
var arrstr2 = arrStr[1].split(".");
event.value = arrstr2[0];

but this works to display the whole string:

var str = event.source.source.documentfilename;
event.value = str;
the results weren't produced in either case. I did find some info on what I am trying to do and it seems there are limitations to consider which (I think) is why the function calls fell over.

Limitations from -> http://acrobatusers.com/tutorials/pr..._stamp_secrets

"There is only one serious limitation to a dynamic-stamp script. Stamp files do not execute JavaScript like a normal PDF file. For example, there are no document or page events. Variables and functions cannot be defined in a document script. The only scripts guaranteed to execute are the calculation scripts of the fields placed on the stamp. Everything necessary to execute this script must be present in the script.

Another, less-important limitation is that the event.rc value is meaningless in a dynamic stamp calculation script. It doesn’t block the field from acquiring the assigned value as it would in a normal calculation script."

Hopefully ya'll like a challenge, thanks for your time and input!
Bent
Bent2BitsŪ is offline   Reply With Quote
Old 02-22-2013, 05:44 AM   PM User | #6
Bent2BitsŪ
New to the CF scene

 
Join Date: Feb 2013
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
Bent2BitsŪ is an unknown quantity at this point
This has been resolved - I didn't realize that JS is case sensitive.

documentFileName should have been used.

Thanks again guys!
Bent2BitsŪ is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:48 AM.


Advertisement
Log in to turn off these ads.