3 Replies Latest reply on Apr 15, 2016 2:35 AM by Ch. Efstathios

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

    Ch. Efstathios Level 1

      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

        • 1. Re: Find and replace all occurences of a string inside the current paragraph.
          Ch. Efstathios Level 1

          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.




          "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)
                  F_ObjHandleT docId = F_ApiGetId(FV_SessionId, FV_SessionId, FP_ActiveDoc);
                  if (!docId)
                  F_TextRangeT insertionPoint = F_ApiGetTextRange(FV_SessionId, docId, FP_TextSelection);
                  if (!insertionPoint.beg.objId)
                  if (insertionPoint.beg.objId != insertionPoint.end.objId)
                  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);
          • 2. Re: Find and replace all occurences of a string inside the current paragraph.
            Ch. Efstathios Level 1

            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?