I have a problem Incremental Character code.
sample
'AAZ' to 'ABB'
gen to 'AAZ' 'AB0' 'AB1' 'AB2' 'AB3' 'AB4' 'AB5' 'AB6' 'AB7' 'AB8' 'AB9' 'ABA' 'ABB'
- Code: Select all
function IncStr(Str: String; Amount: Integer; Index: Integer = -1): String;
const
//MIN_NUM = 48; // '0' 65; // 'A'
//MAX_NUM = 57;
MIN_VAL = 48; //65;
MAX_VAL = 90; // 'Z'
var
Digit, ToAdd, ToCarry: Integer;
begin
if (Index = 0) and (Amount > 0) then
begin
//Result := Char(MIN_VAL + Amount - 1) + Str;
Result := Char(MIN_VAL + Amount - 1) + Str;
Exit;
end;
if Index = -1 then Index := Length(Str);
ToCarry := 0;
Digit := Ord(Str[Index]);
while not (Digit in [MIN_VAL..MAX_VAL]) do
//while not (Digit in [MIN_NUM..MAX_NUM, MIN_VAL..MAX_VAL]) do
begin
Dec(Index);
Digit := Ord(Str[Index]);
end;
ToAdd := Digit + Amount;
while (ToAdd > MAX_VAL) do
begin
Dec(ToAdd, 26);
Inc(ToCarry);
end;
Result := Str;
Result[Index] := Char(ToAdd);
if (ToCarry > 0) then
Result := IncStr(Result, ToCarry, Index - 1);
end;
Thank you in advance for your help
psomkuan