2 Replies Latest reply on Jun 26, 2016 8:09 AM by BaelenIsNice

    How to extract a string from a TextRange


      Dear Experts,

      How can I extract the string "=?utf-8?B?LDvGhsLCBGcmybYk?=" between the coding markers?


      Currently, I am able with the help of code like

      startText = activeDoc.Find(startText.beg, findParams);


      to find the exact start and exact end of the string and I have created the TextRange Base64Text.


      With the instruction activeDoc.TextSelection = Base64Text; I can highlight the text in my document.


      I have tried further with

      Base64Item = activeDoc.GetTextForRange(Base64Text,Constants.FTI_String);

      Base64String = Base64Item.toString();


      But still I am not able to see the string in the object browser of the Toolkit.

      Note that I don't use the method GetText because the string is only a part of the paragraph.


      Thank you for advices.

        • 1. Re: How to extract a string from a TextRange
          4everJang Level 3

          The GetText method, you get an array of Text Items. You need to assemble the string parts of the individual Text Item objects into a single string and then fing the coding markers. Assuming your Base64Text is a valid Text Range, your code should then look like this:


          var saTextItems = activeDoc.GetTextForRange(Base64Text, Constants.FTI_String);

          var i;

          var sFullText = '';

          for( i = 0; i < saTextItems.length; i++ )

               sFullText += saTextItems[i].sdata;

          var iPos1 = sFullText.indexOf( '=?' ) + 2;    /* assuming =? is the start marker and adding 2 to point to the first position behind it */

          var iPos2 = sFullText.indexOf( '?=', iPos1 );   /* adding the start offset for good measure, but that may not be required here */

          var sBase64String = sFullText.substring( iPos1, iPos2 );

          alert( sBase64String );


          Let me know if this works for you. And mark the answer as correct if it does.

          1 person found this helpful
          • 2. Re: How to extract a string from a TextRange
            BaelenIsNice Level 1

            Dear 4everJang,


            Thank you, assembling the strings of each TextItem to a single string worked perfectly.


            // Decode Base64 - Locate the string to be decoded
            Base64Text.beg.obj = startPgf;
            Base64Text.beg.offset = startText.end.offset; 
            Base64Text.end.obj = startPgf;
            Base64Text.end.offset = endText.beg.offset;
            activeDoc.TextSelection = Base64Text;// This instruction only to highlight the string in the document
            Base64Items = activeDoc.GetTextForRange(Base64Text,Constants.FTI_String);
            Base64String = "";
            for (var i = 0; i < Base64Items.length; i++)
            Base64String += Base64Items[i].sdata;


            As the TextRange Base64Text is already corresponding strictly to the string to be recovered, there is not need here to apply the method substring(). Thank you anyway for teaching me the syntax.


            1 person found this helpful