我有一个函数,它通过分隔符拆分字符串: function ExtractURL(url: string; pattern: string; delimiter: char): string;var indexMet, i: integer; urlSplit: TArrayString; delimiterSet: array [0 .. 0] of char;begin delimiterSet[0]
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;
当分隔符是单个字符(‘&’,’|’)时,该功能正常工作.如何将换行符作为分隔符传递.我尝试了#13#10,’#13#10′,sLineBreak,Chr(13)Chr(10),但他们没有用.
@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;