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;
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);
Answers
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:
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,'اثنان مائة','مئتان');
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
i copy your code exactly in the function and i declare the donePos, currPos as Integer
subStr as Text but nothing changed