Better way for this replace?

Hello, I have a string with alphanumeric character and I must replace ‘A’ with ‘11’, ‘B’ with ‘12’, ‘C’ with ‘13’, … 1 letter to 2 numbers … so convertstr doesn’t work I do this : n := STRPOS(str,‘A’); WHILE n<>0 DO BEGIN str := COPYSTR(str,1,n-1)+‘10’+COPYSTR(str,n+1); n := STRPOS(str,‘A’); END; For all letters … It’s not very good and beautiful :confused: Is it a best to do that? Thx ps : It for validating IBAN code.

Try the following (it works, but you may have to change a bit here and there): i: Integer; a: Char; c: Char; res: Text(2 * Length(str)); // this is pseudo-code! res := ‘’; a := ‘A’; FOR i := 1 TO STRLEN(str) DO BEGIN c := str[i]; res += FORMAT(c - a + 11); END; This code assumes that your input string str contains only capital letters, nothing else. Good luck. [8D]

Ok, That is good with some change str is alphanumeric, ther is letters and numbers And it’s +10 not +11 :slight_smile: Thx res := ‘’; a := ‘A’; str := UPPERCASE(str); FOR i := 1 TO STRLEN(str) DO BEGIN c := str[i]; IF (c >= ‘A’) AND (c <= ‘Z’) THEN res := res + FORMAT(c - a + 10) ELSE res := res + FORMAT(c); END; MESSAGE(res);

quote:


And it’s +10 not +11 :slight_smile:


Hey, you said that ‘A’ was ‘11’! [;)]

quote:


IF (c >= ‘A’) AND (c <= ‘Z’) THEN res := res + FORMAT(c - a + 10) ELSE res := res + FORMAT(c);


I’m not familiar with IBAN codes, but with your code above, the input string ‘1A’ would translate into ‘110’, which is also the translation of ‘B0’. Are you sure that a simple FORMAT(c) is sufficient for numbers?

Error in first mail sorry A = 10 B = 11 … And yes, format(c) is good 1A and B0 ar 110 in IBAN Thx

If anyone would that, here a complete function to verify IBAN code VERIFIBAN() and MOD97 function is to calculate big modulus 97 VerifIBAN(s : Code[40]) // Suppression des caractères non alphanumériques str := DELCHR(s,’=’,DELCHR(s,’=’,‘ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789’)); // Les 4 premiers caractères deviennent les 4 derniers str := DELSTR(str,1,4)+COPYSTR(str,1,4); res := ‘’; a := ‘A’; str := UPPERCASE(str); FOR i := 1 TO STRLEN(str) DO BEGIN c := str[i]; IF (c >= ‘A’) AND (c <= ‘Z’) THEN res := res + FORMAT(c - a + 10) ELSE res := res + FORMAT(c); END; IF MOD97(res) <> 1 THEN ERROR(‘Code IBAN %1 incorrect’,s); MOD97(str : Text[100]) reste : Integer i := 1; REPEAT EVALUATE(tmp,COPYSTR(str,i,1)); reste := (reste * 10) + tmp; reste := reste MOD 97; i := i+1; UNTIL i > STRLEN(str);