Skip to main content
Ch__Efstathios
Inspiring
April 13, 2016
Answered

Find and replace all occurences of a string inside the current paragraph.

  • April 13, 2016
  • 3 replies
  • 641 views

I have done so much work and this is my last method to implement.

I want to get from the current active doc the current paragraph and replace all occurances of a string inside that paragraph with another string.

Say for example I have the following paragraph in a document:

"This is a sentence that. This is another sentence. This is also another sentence. The sentence."

If the user opens the document containing the paragraph and goes to that paragraph by clicking in any place in it,, I want to replace e.g.g "another sentence" with "some sentence".

But only in the current active paragraph in the active document.

I have found some code with F_ApiFind and F_ApiGetTextRange to get the current text selection but I cannot think a way to do it.

Message was edited by: Efstathios Chatzikyriakidis

This topic has been closed for replies.
Correct answer Ch__Efstathios

Problem solved people. Thanks.

3 replies

Ch__Efstathios
Inspiring
April 14, 2016

I have written the following function to get the active document and the selection point.

I'm facing two problems:

Problem 1. Whenever I search and replace a text it removes the space after each find match. e.g:

textToFind: "find this"

textToReplace: "replace this"

Example paragraph: "This is a sentence which contains find this and it will be replaced and also this find this will be replaced"

When the method runs it replaces any match of find this in the paragraph but removes the space after the pattern.

Result:

"This is a sentence which contains replace thisand it will be replaced and also this replace thiswill be replaced"

Why is thsi happenning? Can we fix it?

Problem 2. The search and find works in the whole document and starts after the insertion point. I really want to search and replace occurances only inside the current paragraph where the insertion point exists.

Please help.

void FMClient::FindAndReplaceInCurrentParagraph(ConStringT textToFind, ConStringT textToReplace)

p

        F_ObjHandleT docId = F_ApiGetId(FV_SessionId, FV_SessionId, FP_ActiveDoc);

        if (!docId)

        {

            return;

        }

        F_TextRangeT insertionPoint = F_ApiGetTextRange(FV_SessionId, docId, FP_TextSelection);

        if (!insertionPoint.beg.objId)

        {

            return;

        }

        if (insertionPoint.beg.objId != insertionPoint.end.objId)

        {

            return;

        }

        StringT stringToFind = F_StrCopyString(textToFind);

        F_PropValsT findParams = F_ApiAllocatePropVals(2);

        findParams.val[0].propIdent.num = FS_FindText;

        findParams.val[0].propVal.valType = FT_String;

        findParams.val[0].propVal.u.sval = stringToFind;

        findParams.val[1].propIdent.num = FS_FindCustomizationFlags;

        findParams.val[1].propVal.valType = FT_Integer;

        findParams.val[1].propVal.u.ival = FF_FIND_CONSIDER_CASE | FF_FIND_WHOLE_WORD;

        F_TextRangeT searchRange;

        searchRange.beg.objId = searchRange.end.objId = insertionPoint.beg.objId;

        searchRange.beg.offset = searchRange.end.offset = 0;

        FA_errno = FE_Success;

        IntT replaceStringLength = F_ApiStringLen(textToReplace);

        IntT loopCounter = 0;

        searchRange = F_ApiFind(docId, &searchRange.beg, &findParams);

        while (FA_errno == FE_Success && loopCounter++ < 10000)

        {

            F_ApiSetTextRange(FV_SessionId, docId, FP_TextSelection, &searchRange);

            F_ApiClear(docId, 0);

            F_ApiAddText(docId, &searchRange.beg, textToReplace);

            searchRange.beg.offset += replaceStringLength;

            FA_errno = FE_Success;

            searchRange = F_ApiFind(docId, &searchRange.beg, &findParams);

        }

        F_ApiSetTextRange(FV_SessionId, docId, FP_TextSelection, &insertionPoint);

        F_ApiScrollToText(docId, &insertionPoint);

        F_ApiDeallocatePropVals(&findParams);

    }

Ch__Efstathios
Inspiring
April 14, 2016

I have managed to start the process from the start of the current paragraph by calling before find loop starts:

F_ApiSetTextRange(FV_SessionId, docId, FP_TextSelection, &searchRange);

So now I have to find a way to restrict it to search only in the current paragraph and also solve the problem with the removal of the space character.

So Problem 1 and Problem 2 remain. Any proposal?

Ch__Efstathios
Ch__EfstathiosAuthorCorrect answer
Inspiring
April 15, 2016

Problem solved people. Thanks.