关于分隔符:Delphi换行符 | 珊瑚贝

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;

当分隔符为单个字符(\\’

  • 分隔符变量声明为 Char。如果需要多个分隔符,请将分隔符声明为字符串。
  • 删除换行符,然后根据回车符进行提取。
  • 为什么不使用 TStringList?这已经在 sLineBreak 上拆分了字符串。看看这里:stackoverflow.com/questions/15424293/…
  • @SertacAkyuk: SysUtils 单元中有一个 AdjustLineBreaks() 函数用于此目的。
  • @Remy – 谢谢,我以前从未听说过。


@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;

  • 你可以 shorten it 一点。附言对不起,但我已经删除了我的评论,因为我意识到 SplitString 不是你要找的。字符串助手的 Split 有一个带字符串分隔符的重载。


来源:https://www.codenong.com/24206603/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9384.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?