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

Suhail
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..
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..
0
Best Answers
-
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 yoursdonePos:= 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;
1 -
what is the error? or does the client not respond? or do you get a wrong result?5
-
The easiest ReplaceString function without COPYSTR's:
OnRun() aText := 'The quick brown fox jumps over the lazy dog'; aText := ReplaceString(aText,'quick',''); aText := ReplaceString(aText,'brown','black'); aText := ReplaceString(aText,'fox','wolf'); aText := ReplaceString(aText,'lazy','big'); aText := ReplaceString(aText,'dog','rock'); aText := ReplaceString(aText,' ',' '); MESSAGE(aText);
ReplaceString(theText : Text;theOld : Text;theNew : Text) : Text aPosition := STRPOS(theText,theOld); WHILE aPosition > 0 DO BEGIN theText := DELSTR(theText,aPosition,STRLEN(theOld)); theText := INSSTR(theText,theNew,aPosition); aPosition := STRPOS(theText,theOld); END; EXIT(theText);
5
Answers
-
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,'اثنان مائة','مئتان');
0 -
unfortunately, it doesn't work by this way, the function not work0
-
can you post your function, so we could see what is not working there?0
-
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,'اثنان مائة','مئتان');0 -
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 yoursdonePos:= 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;
1 -
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
0 -
im using this function in a report and in Dynamics NAV 20180
-
what is the error? or does the client not respond? or do you get a wrong result?5
-
its working now, sorry this my bad i didnt compile the code correctly, Thank Again Jan87,, i really appreciated0
-
The easiest ReplaceString function without COPYSTR's:
OnRun() aText := 'The quick brown fox jumps over the lazy dog'; aText := ReplaceString(aText,'quick',''); aText := ReplaceString(aText,'brown','black'); aText := ReplaceString(aText,'fox','wolf'); aText := ReplaceString(aText,'lazy','big'); aText := ReplaceString(aText,'dog','rock'); aText := ReplaceString(aText,' ',' '); MESSAGE(aText);
ReplaceString(theText : Text;theOld : Text;theNew : Text) : Text aPosition := STRPOS(theText,theOld); WHILE aPosition > 0 DO BEGIN theText := DELSTR(theText,aPosition,STRLEN(theOld)); theText := INSSTR(theText,theNew,aPosition); aPosition := STRPOS(theText,theOld); END; EXIT(theText);
5
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions