Delphi Newline

I have a function that breaks a string with a separator:

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;

The function works great when the delimiter is the only character ('&', '|'). How to pass a newline character as a separator. I tried C # 13 # 10, '# 13 # 10', sLineBreak, Chr (13) + Chr (10), but they do not work.

+4
source share
1 answer

@TLama first comment on the issue solved my problem. I rewrote the function:

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;
+1
source

Source: https://habr.com/ru/post/1544485/


All Articles