Registriert seit: 11. Okt 2003
Ort: Elbflorenz
44.226 Beiträge
Delphi 12 Athens
|
Zeile aus String extrahieren
9. Aug 2005, 19:44
Diese Funktionen lesen entweder die Erste, oder eine bestimmte Zeile direkt aus einem String aus.
(die Unicode-Varianten erwähne ich jetzt mal nicht ^^)
Zeilenwechsel können als #10 (Linux), oder #13#10 (Windows) gekennzeichnet sein.
Delphi-Quellcode:
Function FirstLine( Const S: AnsiString): AnsiString;
Var i, i2: LongInt;
Begin
i := Length(S);
i2 := Pos(#13, S); If i2 > 0 Then i := i2 - 1;
i2 := Pos(#10, S); If (i2 > 0) and (i >= i2) Then i := i2 - 1;
Result := Copy(S, 1, i);
End;
Function FirstLine( Const S: WideString): WideString;
Var i, i2: Integer;
Begin
i := Length(S);
i2 := Pos(#13, S); If i2 > 0 Then i := i2 - 1;
i2 := Pos(#10, S); If (i2 > 0) and (i >= i2) Then i := i2 - 1;
Result := Copy(S, 1, i);
End;
Function GetLine( Const S: AnsiString; Line: LongInt): AnsiString;
Var i, i2, i3, i4: Integer;
Begin
i := 1; i2 := 1; i3 := 1;
While i <= Length(S) do Begin
If i2 <= i Then Begin i2 := PosEx(#13, S, i); If i2 = 0 Then i2 := Length(S) + 1; End;
If i3 <= i Then Begin i3 := PosEx(#10, S, i); If i3 = 0 Then i3 := Length(S) + 1; End;
If i2 < i3 Then i4 := i2 Else i4 := i3;
If Line = 0 Then Begin
Result := Copy(S, i, i4 - i);
Exit;
End;
i := i4;
If (i <= Length(S)) and (S[i] = #13) Then Inc(i);
If (i <= Length(S)) and (S[i] = #10) Then Inc(i);
Dec(Line);
End;
Result := ' ';
End;
Function GetLine( Const S: WideString; Line: LongInt): WideString;
Var i, i2, i3, i4: Integer;
Begin
i := 1; i2 := 1; i3 := 1;
While i <= Length(S) do Begin
If i2 <= i Then Begin i2 := PosEx(#13, S, i); If i2 = 0 Then i2 := Length(S) + 1; End;
If i3 <= i Then Begin i3 := PosEx(#10, S, i); If i3 = 0 Then i3 := Length(S) + 1; End;
If i2 < i3 Then i4 := i2 Else i4 := i3;
If Line = 0 Then Begin
Result := Copy(S, i, i4 - i);
Exit;
End;
i := i4;
If (i <= Length(S)) and (S[i] = #13) Then Inc(i);
If (i <= Length(S)) and (S[i] = #10) Then Inc(i);
Dec(Line);
End;
Result := ' ';
End;
Natürlich auch in himi's UCC enthalten. (FNSEnt.-.FILES = FNS_Files.pas)
$2B or not $2B
|
|
Zitat
|