• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to extract a string from a TextRange

New Here ,
Jun 25, 2016 Jun 25, 2016

Copy link to clipboard

Copied

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.

TOPICS
Scripting

Views

367

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advocate , Jun 25, 2016 Jun 25, 2016

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.sdata;

var iPos1 = sFullText.indexOf( '=?' ) + 2;    /* assum

...

Votes

Translate

Translate
Advocate ,
Jun 25, 2016 Jun 25, 2016

Copy link to clipboard

Copied

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.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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 26, 2016 Jun 26, 2016

Copy link to clipboard

Copied

LATEST

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.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.

Regards.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines