Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Library: Object-Pascal / Delphi-Language (https://www.delphipraxis.net/35-library-object-pascal-delphi-language/)
-   -   Delphi Zeile aus String extrahieren (https://www.delphipraxis.net/51285-zeile-aus-string-extrahieren.html)

himitsu 9. Aug 2005 18:44


Zeile aus String extrahieren
 
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 Hier im Forum suchenUCC enthalten. (FNSEnt.-.FILES = FNS_Files.pas)


Alle Zeitangaben in WEZ +1. Es ist jetzt 01:53 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz