![]() |
Zahl im String erhöhen
Ich habe in letzter Zeit wahrlich abenteuerliche Code-Beispiele dafüe gesehen, daher hier eine kurze und einfach Variante...
Es geht darum, eine Zahl, die ganz vorne in einem String steht zu erhöhen. Beispiel: '27 Liter' um 13 erhöhen ergäbe dann '40 Liter'
Delphi-Quellcode:
Das einzige was dazu gebraucht wird sind die SysUtils (StrToInt/IntToStr)...
function IncLeadingNumInStr(S: String; IncBy: Integer): String;
var Buf: String; begin Buf:=''; while S[1] in ['0'..'9'] do begin Buf:=Buf+S[1]; Delete(S,1,1); if Length(S)=0 then Break; end; if Buf='' then Buf:='0'; Result:=IntToStr(StrToInt(Buf)+IncBy)+S; end; himitsu hat diese, optimierte, Version vorgeschlagen:
Delphi-Quellcode:
[edit=Chakotay1308]himitsu's Code hinzugefügt. Mfg, Chakotay1308[/edit]
function IncLeadingNumInStr(S: String; IncBy: Integer): String;
var i: Integer; begin i:=0; while (i < Length(S)) and (S[i + 1] in ['0'..'9']) do inc(i); Result := IntToStr(StrToIntDef(Copy(S, 1, i),0) + IncBy) + Copy(S, i + 1, MaxInt); end; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 07:43 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz