Hi Mike.

My function was ment to be a bit more generic, it was supposed to 
increment the first group of digits in any given string, maintaining 
it's length and maintaining it's position in the original string. Since 
I did not try the code in Delphi I did not notice the first "1" in your 
string. Any way, if you already know you'll be incrementing the last 
three digits and nothing else, my code is probably too much. You can get 
away with something allot simpler, like:

function IncEndDigits(str:string):string;
var len:Integer;
    prefix, num:string;
begin
  len := Length(str);
  num := System.Copy(str, len-2, 3);
  prefix := System.Copy(str, 1, len-3);
  Result := prefix + Format('%.3d', [StrToIntDef(num,0)+1]);
end;

OR the contracted version:

function IncEndDigits(str:string):string;
begin
  Result := System.Copy(str, 1, Length(str)-3) + Format('%.3d', 
[StrToIntDef(System.Copy(str, Length(str)-2, 3),0)+1]);
end;

Please note: this code will treat the last 3 chars of any string as 
numbers and attempt a str-to-int conversion. If those last three chars 
are not a number they will be treated as zero. Please also note this 
function pretty dumb: "AN999" would be incremented to "AN1000" but 
"AN1999" would be incremented to "AN11000". That is, the function only 
sees the last three digits, no matter what!

Mike Lucek wrote:
> Thanks Cosmin & Grant. Both your solutions didn't work. Cosmin, the number
> incremented was the wrong one. I needed the last three characters to be
> incremented in AB-CDE-FG1-004, your's incremented the integer in FG. That
> is, FG1 became FG2.
>
> However your function was put to use, so thank you. What I did was extracted
> the last three characters in the string and passed it to your function. This
> then correctly returned the result of AB-CDE-FG1-005.
>
> Mike
>
>
> _______________________________________________
> Delphi mailing list -> [email protected]
> http://www.elists.org/mailman/listinfo/delphi
>
>   

_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi

Reply via email to