简体   繁体   English

如何在delphi中检查Tstringlist的长度

[英]How to check length of Tstringlist in delphi

This is what I am trying to do. 这就是我想要做的。 I have a Tstringlist, for a name. 我有一个Tstringlist,一个名字。 If the name is in a format DOE, JOHN, NMI, I want it to split the name into 3 different strings. 如果名称格式为DOE,JOHN,NMI,我希望它将名称拆分为3个不同的字符串。
But the problem is, what if there is no middle initial. 但问题是,如果没有中间的首字母怎么办? Or First name. 或名字。 Like it could be just DOE, Then the last two lines are out of bounds. 就像它可能只是DOE一样,然后最后两行超出界限。 And the program crashes. 程序崩溃了。 What is the best solution? 什么是最好的解决方案?

var ptname, physname: Tstringlist;

if pos(',',Msg.Grp2[0].ObsReq[0].OrderingProviderFamilyName) > 0 then // split it if it has a comma
begin
  physname := TstringList.Create;
  physname.CommaText := Msg.Grp2[0].ObsReq[0].OrderingProviderFamilyName;
  Parameters.ParamByName('@OrderingLastNameOBR16').Value := physname[0];
  Parameters.ParamByName('@OrderingFirstNameOBR16').Value := physname[1];
  Parameters.ParamByName('@OrderingMiddleNameOBR16').Value := physname[2];
  physname.Free;
end

Use TStringList.Count . 使用TStringList.Count

  physname := TstringList.Create;
  physname.CommaText := Msg.Grp2[0].ObsReq[0].OrderingProviderFamilyName;
  if physname.Count > 0 then
  begin
    Parameters.ParamByName('@OrderingLastNameOBR16').Value := physname[0];
    if physname.Count > 1 then
    begin
      Parameters.ParamByName('@OrderingFirstNameOBR16').Value := physname[1];
      if physname.Count > 2 then
      begin
        Parameters.ParamByName('@OrderingMiddleNameOBR16').Value := physname[2];
      end;
    end;
  end;
  physname.Free;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM