Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,438
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    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.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,438
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    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.

  • Users who have thanked Old Pedant for this post:

    Bent2Bits® (02-21-2013)

  • #4
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    224
    Thanks
    0
    Thanked 32 Times in 30 Posts
    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]);

  • Users who have thanked 007julien for this post:

    Bent2Bits® (02-21-2013)

  • #5
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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

  • #6
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    This has been resolved - I didn't realize that JS is case sensitive.

    documentFileName should have been used.

    Thanks again guys!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •