Registriert seit: 29. Mai 2002
37.621 Beiträge
Delphi 2006 Professional
|
AW: Zeichen aus einem String filtern/löschen
30. Nov 2011, 14:48
Was für ein Buffer?
Delphi-Quellcode:
program Project2;
{$APPTYPE CONSOLE}
uses
SysUtils;
const
LINE = '[TimeStamp]:<Indent1><Indent2>LogMessage';
function ExtractTimeStamp(LogLine: string): string;
var
Start, Stop: Integer;
begin
Start := Pos('[', LogLine);
Stop := Pos(']', LogLine);
Result := Copy(LogLine, Start + 1, Stop - Start - 1);
end;
function ExtractIndent1(LogLine: string): string;
var
Start, Stop: Integer;
begin
Start := Pos('<', LogLine);
Stop := Pos('>', LogLine);
Result := Copy(LogLine, Start + 1, Stop - Start - 1);
end;
function ExtractIndent2(LogLine: string): string;
var
Start, Stop: Integer;
Start2, Stop2: Integer;
begin
Start := Pos('<', LogLine);
Start2 := PosEx('<', LogLine, Start + 1); // +1, sonst findet er das erste Zeichen wieder
Stop := Pos('>', LogLine);
Stop2 := PosEx('>', LogLine, Stop + 1);
Result := Copy(LogLine, Start2 + 1, Stop2 - Start2 -1);
end;
function ExtractLogMsg(LogLine: string): string;
var
Start, Start2: Integer;
begin
Start := Pos('>', LogLine);
Start2 := PosEx('>', LogLine, Start + 1); // +1, sonst findet er das erste Zeichen wieder
Result := Copy(LogLine, Start2 + 1, Length(LogLine) - Start2);
end;
procedure SplitLogLine(LogLine: string; out TimeStamp: string; out Indent1: string; out Indent2: string; out LogMsg: string);
begin
TimeStamp := ExtractTimeStamp(LogLine);
Indent1 := ExtractIndent1(LogLine);
Indent2 := ExtractIndent2(LogLine);
LogMsg := ExtractLogMsg(LogLine);
end;
var
TimeStamp: string;
Indent1: string;
Indent2: string;
LogMsg: string;
begin
SplitLogLine(LINE, TimeStamp, Indent1, Indent2, LogMsg);
Writeln(TimeStamp);
Writeln(Indent1);
Writeln(Indent2);
Writeln(LogMsg);
Readln;
end.
(Dauer 10 Minuten, inklusive starten der IDE und suchen nach einer PosEx Funktion hier im Forum.)
Die Fallunterscheidung überlasse ich dir.
Michael Ein Teil meines Codes würde euch verunsichern.
Geändert von Luckie (30. Nov 2011 um 14:53 Uhr)
|
|
Zitat
|