Delphi换行符

时间:2014-06-13 13:32:36

标签: delphi delimiter string-split

我有一个用分隔符分割字符串的函数:

function ExtractURL(url: string; pattern: string; delimiter: char): string;
var
  indexMet, i: integer;
  urlSplit: TArray<String>;
  delimiterSet: array [0 .. 0] of char;
begin
  delimiterSet[0] := delimiter;
  urlSplit := url.Split(delimiterSet);
  Result := '';

  for i := 0 to Length(urlSplit) - 1 do
  begin
    if urlSplit[i].Contains(pattern) then
    begin
      indexMet := urlSplit[i].LastIndexOf('=') + 1; // extracts pairs key=value
      Result := urlSplit[i].Substring(indexMet);
      Exit;
    end;
  end;
end;

当分隔符是单个字符('&amp;','|')时,该函数可以正常工作。如何将换行符作为分隔符传递。我试过#13#10,'#13#10',sLineBreak,Chr(13)+ Chr(10),但它们不起作用。

1 个答案:

答案 0 :(得分:1)

@TLama首先评论这个问题解决了我的问题。我重写了这个功能:

function ExtractURL(url: string; pattern: string; delimiter: string): string;
var
  indexMet, i: integer;
  urlSplit: TStringDynArray;
begin
  // note that the delimiter is a string, not a char
  urlSplit := System.StrUtils.SplitString(url, delimiter);
  result := '';

  for i := 0 to Length(urlSplit) - 1 do
  begin
    if urlSplit[i].Contains(pattern) then
    begin
      indexMet := urlSplit[i].LastIndexOf('=') + 1;
      result := urlSplit[i].Substring(indexMet);
      Exit;
    end;
  end;
end;