Friday, August 3, 2018

Simple tool to replace a string or character in Microsoft Dynamics NAV




Ever got a task to replace a string or character in a string in Microsoft Dynamics NAV but unable to find the simplest solution for this problem.
Don't worry I have a simple solution for this task.

I have created a Codeunit which you can find below:


OBJECT Codeunit 50003 String Replaced
{
  OBJECT-PROPERTIES
  {
    Date=03-08-18;
    Time=12:10:48 PM;
    Modified=Yes;
    Version List=;
  }
  PROPERTIES
  {
    OnRun=BEGIN
            OldStringG    := 'OldString';
            ReplaceWithG  := 'New';
            SearchStringG := 'Old';

            NewStringG := ReplaceString(OldStringG,SearchStringG,ReplaceWithG);
            MESSAGE(OldStringG);
            MESSAGE(NewStringG);
          END;

  }
  CODE
  {
    VAR
      OldStringG@1000000001 : Text[250];
      SearchStringG@1000000002 : Text[250];
      ReplaceWithG@1000000003 : Text[250];
      NewStringG@1000000000 : Text[250];

    PROCEDURE ReplaceString@1000000007(OldStringP@1000000000 : Text[250];SearchStringP@1000000001 : Text[250];ReplaceWithP@1000000002 : Text[250]) NewStringL : Text[250];
    BEGIN

      NewStringL:= DELSTR(OldStringP,STRPOS(OldStringG,SearchStringP)) + ReplaceWithP + COPYSTR(OldStringP,STRPOS(OldStringP,SearchStringP) + STRLEN(SearchStringP));
    END;

    BEGIN
    END.
  }
}


You can modify this code according to your needs to make it suitable for your requirement.

Enjoyed the topic, Please do subscribe to our newsletter to get updated with every post without a miss.




Thanks for reading,



Shubin Dongre