Options

how to make multi variable for Replace String in C/AL code

SuhailSuhail Member Posts: 23
Hi guys,

Im new in c/al code, i build a Replace String Function and im trying to save the output in a Variable "ReplaceText" and use this variable for all conditions i have but it dose't work for all, just for one condition or one line, to be clear for you this my variables:

ReplaceText := ReplaceString(TEXT,'واحد مائة','مائة');
ReplaceText := ReplaceString(TEXT,'اثنان مائة','مائتان');
ReplaceText := ReplaceString(TEXT,'اثنان مائة','مئتان');

if you noted i repeat my variable to take all conditions but it doesn't work, if i remove the two line and i kept just first line its work perfectly, please help..

Best Answers

  • Options
    Jan87Jan87 Member Posts: 26
    Answer ✓
    I tested your function.

    You have the problem, that you produce a infinite loop, because the "FindWhat" is inside the "ReplaceText" and so you never leave the "WHILE".

    You have do to something like:

    donePos, currPos are Integer
    subStr is Text
    other variables I renamed like yours
    donePos:= STRPOS(String,FindWhat);
    currPos := donePos;
    WHILE (currPos> 0) DO BEGIN
      String := COPYSTR(String,1,donePos-1) + ReplaceWith + COPYSTR(String,donePos+STRLEN(FindWhat));
      subStr := COPYSTR(String,donePos+STRLEN(ReplaceWith));
      currPos := STRPOS(subStr,FindWhat);
      donePos:= donePos+ currPos + STRLEN(ReplaceWith) - 1;
    END;
    NewString := String;
    

Answers

  • Options
    Jan87Jan87 Member Posts: 26
    Hi Suhail,

    not sure what the context is but I think you forgot to use the result from the first in the following calls.

    so it should be:
    ReplaceText := ReplaceString(TEXT,'واحد مائة','مائة');
    ReplaceText := ReplaceString(ReplaceText,'اثنان مائة','مائتان');
    ReplaceText := ReplaceString(ReplaceText,'اثنان مائة','مئتان');
    
  • Options
    SuhailSuhail Member Posts: 23
    unfortunately, it doesn't work by this way, the function not work
  • Options
    Jan87Jan87 Member Posts: 26
    can you post your function, so we could see what is not working there?
  • Options
    SuhailSuhail Member Posts: 23
    Sure,

    ReplaceString(String : Text[300];FindWhat : Text[300];ReplaceWith : Text[300]) NewString : Text[300]

    WHILE STRPOS(String,FindWhat) > 0 DO
    String := DELSTR(String,STRPOS(String,FindWhat)) + ReplaceWith + COPYSTR(String,STRPOS(String,FindWhat) + STRLEN(FindWhat));
    NewString := String;


    EDM.InitTextVariable; EDM.NumberInWords(Amount),'ريال سعودي','هللة');
    TEXT := AmountInWords;
    ReplaceText := ReplaceString(TEXT,'واحد مائة','مائة');
    ReplaceText := ReplaceString(ReplaceText,'اثنان مائة','مائتان');
    ReplaceText := ReplaceString(ReplaceText,'اثنان مائة','مئتان');
  • Options
    Jan87Jan87 Member Posts: 26
    Answer ✓
    I tested your function.

    You have the problem, that you produce a infinite loop, because the "FindWhat" is inside the "ReplaceText" and so you never leave the "WHILE".

    You have do to something like:

    donePos, currPos are Integer
    subStr is Text
    other variables I renamed like yours
    donePos:= STRPOS(String,FindWhat);
    currPos := donePos;
    WHILE (currPos> 0) DO BEGIN
      String := COPYSTR(String,1,donePos-1) + ReplaceWith + COPYSTR(String,donePos+STRLEN(FindWhat));
      subStr := COPYSTR(String,donePos+STRLEN(ReplaceWith));
      currPos := STRPOS(subStr,FindWhat);
      donePos:= donePos+ currPos + STRLEN(ReplaceWith) - 1;
    END;
    NewString := String;
    
  • Options
    SuhailSuhail Member Posts: 23
    Thanks Jan87 for your effort but still doesn't work :(

    i copy your code exactly in the function and i declare the donePos, currPos as Integer
    subStr as Text but nothing changed
  • Options
    SuhailSuhail Member Posts: 23
    im using this function in a report and in Dynamics NAV 2018
  • Options
    SuhailSuhail Member Posts: 23
    its working now, sorry this my bad i didnt compile the code correctly, Thank Again Jan87,, i really appreciated <3
  • Options
    JuhlJuhl Member Posts: 724
    Use .NET
    Follow me on my blog juhl.blog
Sign In or Register to comment.