Welcome

首页 / 软件开发 / Delphi / Delphi中使用链式代码

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

//譬如要把 3.1415926 中的 141 提取为一个整数:varnum: Integer;beginnum := Pi.ToString().Split(["."])[1].Substring(0,3).ToInteger(); // 输入 . 后, 记得使用 Ctrl+Space 提示代码ShowMessage(num.ToString()); // 141end;
我是在写类似下面程序时开始使用的:

{程序要求从下面的文本中提取 A: B: ... 后面的文本到指定的变量:A: qwertyuiopB: wertyuiopaC: ertyuiopasqwertyuiopasdD: rtyuiopasd...}uses System.Character; // <-- IsInArrayconstFText = "A: qwertyuiop"#13#10 +"B: wertyuiopa"#13#10 +"C: ertyuiopas"#13#10 +"qwertyuiopasd"#13#10 +"D: rtyuiopasd"#13#10; procedure TForm1.Button1Click(Sender: TObject);varList: TStringList;A, B, C, D: string;str: string;beginList := TStringList.Create;List.Text := FText; for str in List dobeginif (str.Length > 2) and str[2].IsInArray([":"]) thenbegincase 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;