Delphi 的链式代码


本文整理自网络,侵删。

 Delphi 的链式代码

有了一系列的 Helper, Delphi 也可以使用链式代码了.
--------------------------------------------------------------------------------


//譬如要把 3.1415926 中的 141 提取为一个整数:
var
  num: Integer;
begin
  num := Pi.ToString().Split(['.'])[1].Substring(0,3).ToInteger(); // 输入 . 后, 记得使用 Ctrl+Space 提示代码
  ShowMessage(num.ToString()); // 141
end;
--------------------------------------------------------------------------------

我是在写类似下面程序时开始使用的:
--------------------------------------------------------------------------------


{程序要求从下面的文本中提取 A: B: ... 后面的文本到指定的变量:
A: qwertyuiop
B: wertyuiopa
C: ertyuiopas
qwertyuiopasd
D: rtyuiopasd
...
}

uses System.Character; // <-- IsInArray

const
  FText = 'A: qwertyuiop'#13#10 +
          'B: wertyuiopa'#13#10 +
          'C: ertyuiopas'#13#10 +
          'qwertyuiopasd'#13#10 +
          'D: rtyuiopasd'#13#10;

procedure TForm1.Button1Click(Sender: TObject);
var
  List: TStringList;
  A, B, C, D: string;
  str: string;
begin
  List := TStringList.Create;
  List.Text := FText;

  for str in List do
  begin
    if (str.Length > 2) and str[2].IsInArray([':']) then
    begin
      case str[1] of
        'A': A := str.Substring(2).Trim; // 这里用上了链式代码
        'B': B := str.Substring(2).Trim;
        'C': C := str.Substring(2).Trim;
        'D': D := str.Substring(2).Trim;
      end;
    end;
  end;

  List.Free;

  ShowMessageFmt('%s; %s; %s; %s', [A, B, C, D]);
end;
--------------------------------------------------------------------------------

相关阅读 >>

Delphi 操作系统时间与web标准时间校正

Delphi 泛型,存放n张图片

Delphi 让子窗体显示在任务栏上

Delphi windows 编程[5] - 学习窗体生成的过程五

aes.pas 和 elaes.pas

推荐一套免费跨平台的Delphi 哈希及加密算法库

Delphi系统默认语言与系统支持的语言列表

Delphi基于高斯-拉普拉斯算子的图像边缘检测

Delphi 判断当前程序是否是活动窗口

Delphi xe [dcc32 fatal error] f2039 could not create output file 问题的解决

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...