Delphi newline character
我有一个用分隔符分割字符串的函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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; |
当分隔符为单个字符(\\'
@TLama 对这个问题的第一条评论解决了我的问题。我重写了函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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; |