Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
Delphi 10 Seattle Enterprise
|
AW: An welcher Position war/ist die Variable im Template
25. Jul 2016, 23:39
Oder eben nach alter Väter Sitte:
Delphi-Quellcode:
program Project4;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
function GetCounterFromTemplatedString( const Str, Template: string ): Integer;
const
CCounter = ' %counter%';
CYear = ' %year%';
CYearDefault = ' 0000';
var
tmp : string;
lFirst, lTail, lLength: Integer;
begin
tmp := Template.Replace( CYear, CYearDefault );
lFirst := tmp.IndexOf( CCounter );
if lFirst = -1
then
raise Exception.Create( ' Fehlermeldung' );
lTail := tmp.Length - lFirst - CCounter.Length;
lLength := Str.Length - lFirst - lTail;
tmp := Str.Substring( lFirst, lLength );
Result := Integer.Parse( tmp );
end;
procedure Check( const Str, Template: string; expected: Integer );
begin
if GetCounterFromTemplatedString(str,template) <> expected then
raise Exception.Create(' Fehlermeldung');
end;
procedure Test;
begin
Check( ' 1001030812016', ' 100%counter%3081%year%', 10 );
Check( ' 10012530812016', ' 100%counter%3081%year%', 125 );
Check( ' 100217830812016', ' 100%counter%3081%year%', 2178 );
Check( ' 10047930812016', ' 100%counter%3081%year%', 479 );
Check( ' P12016', ' P%counter%%year%', 1 );
Check( ' P1002016', ' P%counter%%year%', 100 );
end;
begin
try
Test;
except
on E: Exception do
Writeln( E.ClassName, ' : ', E. Message );
end;
ReadLn;
end.
PS: leider allgemeingültig
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
Geändert von Sir Rufo (25. Jul 2016 um 23:41 Uhr)
|
|
Zitat
|