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: ... 后面的文本到指定的变量:
Aqwertyuiop
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 pagecontrol1 另类隐藏页的方法

Delphi post数据与对应的接收方式

Delphi directx简单应用

Delphi 从网页里下载图片的程序

Delphi 更改消息对话框中的按钮标题

Delphi 利用unigui中的tunipagecontrol实现多页面

Delphi图像数据压缩解压缩实例

Delphi idhttp post 普通提交乱码处理

Delphi system.sysutils.tmarshaller 与 system.tmarshal

Delphi代码实现窗口最小化,最大化,关闭消息发送

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



打赏

取消

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

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

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

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

评论

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

    暂无评论...