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 datasnap 初步入门使用总结

Delphi 调用打印软件代码

Delphi 组件值实现增减

Delphi 数据库重置用户密码制作

Delphi什么是thttpclient?

Delphi编程禁止用户关闭操作系统

Delphi 图像灰度化处理

Delphi adoquery1数据表参数调用

Delphi动态创建一个ipedit控件

Delphi 读文件到十六进制的函数(Delphi 7 下可用)

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



打赏

取消

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

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

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

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

评论

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