Replace Character by Number in String

Hi everybody, How can I do what is in the subject Help very much appreciated Tanks

Hi Christophe, Assuming that this is what you mean: str1 := ‘abc’; str2 := CONVERTSTR(str1, ‘b’, ‘1’); str2 contains ‘a1c’ Regards, Reijer

Yes quite of but in that case A → 10 and… Z -->35 Thanks

As far as I know there isn’t a standard function to do this. This is a function I wrote some time ago to get the result you want. Replace(VAR String1 : Text[255];String2 : Text[255];String3 : Text[255]) AantalPos := STRPOS(String1, String2); IF AantalPos <> 0 THEN BEGIN IF AantalPos > 1 THEN Deel1 := COPYSTR(String1, 1, AantalPos - 1); IF AantalPos <> STRLEN(String1) - (STRLEN(String2) - 1) THEN Deel2 := COPYSTR(String1, AantalPos + STRLEN(String2)); String1 := Deel1 + String3 + Deel2; END;

Thanks very much I just have to add a repeat until as i must change all occurence of the character Thanks again

If it is just a one off run you want to do you can just run a table or form select the field and use replace from the tool bar. Or the 2 string functions NewString := DELSTR(String, Position [, Length]); NewString := INSSTR(String, SubString, Position); Edited by - David Cox on 2001 Sep 13 21:28:33

Here is a complete Replace-Function:


Procedure Replace (stOrig, stOld, stNew : Text250) : Text250
st := stOrig;
p := STRPOS (st, stOld);
WHILE p <> 0 DO BEGIN
  st := DELSTR (st, p, STRLEN (stOld));
  st := INSSTR (st, stNew, p);
  p := STRPOS (st, stOld);
END;
EXIT (st);

It replaces all occurences of stOld in stOrig and with stNew. Such as:


Replace ('Hello World', 'll', 'xx'); 

will result in ‘Hexxo World’ Attention! The procedure will fail if stOld is contained within stNew: stOld = ‘ab’; stNew = ‘abc’; ------- With best regards from Switzerland Marcus Fabian Edited by - fabian on 2001 Sep 14 00:54:17

Thanks to everybody PS Marcus you don’t need your variable ‘st’

Hi Marcus,

I develop myself a solution, and I think that’s works in all possible cases… Take a look:

Procedure Replace (stOrig : Text[250];stOld : Text[250];stNew : Text[250]) : Text[250]

IF stNew = stOld THEN
EXIT(stOrig);

i := 1;

len := STRLEN(stOrig);
len2 := STRLEN(stOld);

REPEAT

IF COPYSTR(stOrig,i,1) = COPYSTR(stOld,1,1) THEN
BEGIN
IF COPYSTR(stOrig,i,len2) = stOld THEN
BEGIN
st := st + stNew;
i := i + len2-1;
END
ELSE
st := st + COPYSTR(stOrig,i,1);
END
ELSE
st := st + COPYSTR(stOrig,i,1);
i := i + 1;
UNTIL i = len+1;

EXIT(st);

Best regards from Portugal

Nuno Soares