开发者

How to check length of Tstringlist in delphi

开发者 https://www.devze.com 2023-02-15 02:58 出处:网络
This is what I am trying to do.I have a Tstringlist, for a name.If the name is in a format DOE, JOHN, NMI, I want it to split the name into 3 different strings.

This is what I am trying to do. I have a Tstringlist, for a name. If the name is in a format DOE, JOHN, NMI, I want it to split the name into 3 different strings.

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. 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('@OrderingMiddleNa开发者_如何学运维meOBR16').Value := physname[2];
  physname.Free;
end


Use 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;
0

精彩评论

暂无评论...
验证码 换一张
取 消