Einzelnen Beitrag anzeigen

hathor
(Gast)

n/a Beiträge
 
#4

Re: String an bestimmtem Zeichen Splitten

  Alt 20. Aug 2008, 16:55
Delphi-Quellcode:
function SplitStr(const S: string; Delim: Char; out S1, S2: string): Boolean;
  {Splits the string S at the first occurence of delimiter character Delim and
  sets S1 to the sub-string before Delim and S2 to substring following Delim.
  If Delim is found in string True is returned, while if Delim is not in string
  False is returned, S1 is set to S and S2 is set to ''.}

var
  DelimPos: Integer; // position of delimiter in source string
begin
  // Find position of first occurence of delimter in string
  DelimPos := SysUtils.AnsiPos(Delim, S);
  if DelimPos > 0 then
  begin
    // Delimiter found: do split and return True
    S1 := Copy(S, 1, DelimPos - 1);
    S2 := Copy(S, DelimPos + 1, MaxInt);
    Result := True;
  end
  else
  begin
    // Delimeter not found: return false and set S1 to whole string
    S1 := S;
    S2 := '';
    Result := False;
  end;
end;
  Mit Zitat antworten Zitat