Delphi 如何按拆分字符串或分隔符拆分字符串?

Delphi 如何按拆分字符串或分隔符拆分字符串?,delphi,Delphi,我有客户端和服务器应用程序。 我在两侧使用应用程序连接。 在服务器端,我使用SQLite数据库 在客户端没有数据库; 客户端正在从服务器数据库获取数据。 有了服务器代码,我觉得还可以。客户有问题,我不能拆分!!!我在客户端编写代码。谁能告诉我,我的错在哪里??? 如何拆分r3.Text:=AResource.Value.AsString 服务器端代码: procedure TfServerPage.tAProfileResourceReceived(const Sender: TObject;

我有客户端和服务器应用程序。 我在两侧使用应用程序连接。 在服务器端,我使用SQLite数据库 在客户端没有数据库; 客户端正在从服务器数据库获取数据。 有了服务器代码,我觉得还可以。客户有问题,我不能拆分!!!我在客户端编写代码。谁能告诉我,我的错在哪里??? 如何拆分r3.Text:=AResource.Value.AsString


服务器端代码:

procedure TfServerPage.tAProfileResourceReceived(const Sender: TObject;
  const AResource: TRemoteResource);
  var
    result:string;
    r1,r2,r3:TstringList;
  begin

if AResource.Hint='List' then
        begin
          //ShowMessage(AResource.Value.AsString);
            r1:=TStringList.Create;
            r2:=TStringList.Create;
            r3:=TStringList.Create;
            rQuery.Open;
             while not rQuery.Eof do
                begin
                 r1.add(rQuery.FieldByName('username').AsString);
                 r2.add(rQuery.FieldByName('password').AsString);
                 r3.add(rQuery.FieldByName('nickname').AsString);
                 rQuery.Next;
                end;
            result:=r1.Text+#13+r2.Text+#13+r3.text;
           tAProfile.SendString(tManager.RemoteProfiles.First,'List2',result);

        end;
end;
procedure TfAuth.tAProfileResourceReceived(const Sender: TObject;
  const AResource: TRemoteResource);
var i:integer;
r3,s1,s2,s3:TStringList;
begin


    if AResource.Hint='List2' then
      begin
        r3:=TStringList.Create;
        r3.Text:=AResource.Value.AsString;    // <-From Server

        s1:=TStringList.Create;
        s2:=TStringList.Create;
        s3:=TStringList.Create;

      s1.Text:=SplitString(r3.Text,#13)[0]; //Split username
      s2.Text:=SplitString(r3.Text,#13)[1]; //password
      s3.Text:=SplitString(r3.Text,#13)[2]; //nickname

       for i := 0 to r3.Count do
           begin
            StringGrid1.Cells[0,i]:=s1[i];   
            StringGrid1.Cells[1,i]:=s2[i];  
            StringGrid1.Cells[2,i]:=s3[i]; 
           end;
    end;

end;
 result:=r1.Text+#0+r2.Text+#0+r3.text;
 s1.Text:=SplitString(r3.Text,#0)[0];

  s2.Text:=SplitString(r3.Text,#0)[1];

  s3.Text:=SplitString(r3.Text,#0)[2];
客户端代码:

procedure TfServerPage.tAProfileResourceReceived(const Sender: TObject;
  const AResource: TRemoteResource);
  var
    result:string;
    r1,r2,r3:TstringList;
  begin

if AResource.Hint='List' then
        begin
          //ShowMessage(AResource.Value.AsString);
            r1:=TStringList.Create;
            r2:=TStringList.Create;
            r3:=TStringList.Create;
            rQuery.Open;
             while not rQuery.Eof do
                begin
                 r1.add(rQuery.FieldByName('username').AsString);
                 r2.add(rQuery.FieldByName('password').AsString);
                 r3.add(rQuery.FieldByName('nickname').AsString);
                 rQuery.Next;
                end;
            result:=r1.Text+#13+r2.Text+#13+r3.text;
           tAProfile.SendString(tManager.RemoteProfiles.First,'List2',result);

        end;
end;
procedure TfAuth.tAProfileResourceReceived(const Sender: TObject;
  const AResource: TRemoteResource);
var i:integer;
r3,s1,s2,s3:TStringList;
begin


    if AResource.Hint='List2' then
      begin
        r3:=TStringList.Create;
        r3.Text:=AResource.Value.AsString;    // <-From Server

        s1:=TStringList.Create;
        s2:=TStringList.Create;
        s3:=TStringList.Create;

      s1.Text:=SplitString(r3.Text,#13)[0]; //Split username
      s2.Text:=SplitString(r3.Text,#13)[1]; //password
      s3.Text:=SplitString(r3.Text,#13)[2]; //nickname

       for i := 0 to r3.Count do
           begin
            StringGrid1.Cells[0,i]:=s1[i];   
            StringGrid1.Cells[1,i]:=s2[i];  
            StringGrid1.Cells[2,i]:=s3[i]; 
           end;
    end;

end;
 result:=r1.Text+#0+r2.Text+#0+r3.text;
 s1.Text:=SplitString(r3.Text,#0)[0];

  s2.Text:=SplitString(r3.Text,#0)[1];

  s3.Text:=SplitString(r3.Text,#0)[2];
过程TfAuth.tAProfileResourceReceived(常量发送方:TObject;
const AResource:tremotesource);
varⅠ:整数;
r3,s1,s2,s3:TStringList;
开始
如果是source.Hint='List2',则
开始
r3:=TStringList.Create;

r3.Text:=AResource.Value.AsString;// 在服务器端,您将db记录中的字段添加到3个字符串列表中,r1-用户名、r2-密码和r3-昵称。字符串列表默认使用回车换行符(或#13#10)作为行尾字符

在r1.Text、r2.Text和r3.Text之间用#13连接这些字符串列表的文本时,基于#13拆分回字符串列表是错误的。您应该使用其他字符作为字符串列表的分隔符。尝试使用例如#11(垂直选项卡)

我还将在客户端使用
s:string
而不是
r3:TStringList

您正在泄漏内存,应该释放
TStringList


更好的是,如果要传输更大的数据记录,则应该考虑使用XML或JSON。它们提供了以更方便的方式序列化数据的方法

服务器端代码:

procedure TfServerPage.tAProfileResourceReceived(const Sender: TObject;
  const AResource: TRemoteResource);
  var
    result:string;
    r1,r2,r3:TstringList;
  begin

if AResource.Hint='List' then
        begin
          //ShowMessage(AResource.Value.AsString);
            r1:=TStringList.Create;
            r2:=TStringList.Create;
            r3:=TStringList.Create;
            rQuery.Open;
             while not rQuery.Eof do
                begin
                 r1.add(rQuery.FieldByName('username').AsString);
                 r2.add(rQuery.FieldByName('password').AsString);
                 r3.add(rQuery.FieldByName('nickname').AsString);
                 rQuery.Next;
                end;
            result:=r1.Text+#13+r2.Text+#13+r3.text;
           tAProfile.SendString(tManager.RemoteProfiles.First,'List2',result);

        end;
end;
procedure TfAuth.tAProfileResourceReceived(const Sender: TObject;
  const AResource: TRemoteResource);
var i:integer;
r3,s1,s2,s3:TStringList;
begin


    if AResource.Hint='List2' then
      begin
        r3:=TStringList.Create;
        r3.Text:=AResource.Value.AsString;    // <-From Server

        s1:=TStringList.Create;
        s2:=TStringList.Create;
        s3:=TStringList.Create;

      s1.Text:=SplitString(r3.Text,#13)[0]; //Split username
      s2.Text:=SplitString(r3.Text,#13)[1]; //password
      s3.Text:=SplitString(r3.Text,#13)[2]; //nickname

       for i := 0 to r3.Count do
           begin
            StringGrid1.Cells[0,i]:=s1[i];   
            StringGrid1.Cells[1,i]:=s2[i];  
            StringGrid1.Cells[2,i]:=s3[i]; 
           end;
    end;

end;
 result:=r1.Text+#0+r2.Text+#0+r3.text;
 s1.Text:=SplitString(r3.Text,#0)[0];

  s2.Text:=SplitString(r3.Text,#0)[1];

  s3.Text:=SplitString(r3.Text,#0)[2];
客户端代码:

procedure TfServerPage.tAProfileResourceReceived(const Sender: TObject;
  const AResource: TRemoteResource);
  var
    result:string;
    r1,r2,r3:TstringList;
  begin

if AResource.Hint='List' then
        begin
          //ShowMessage(AResource.Value.AsString);
            r1:=TStringList.Create;
            r2:=TStringList.Create;
            r3:=TStringList.Create;
            rQuery.Open;
             while not rQuery.Eof do
                begin
                 r1.add(rQuery.FieldByName('username').AsString);
                 r2.add(rQuery.FieldByName('password').AsString);
                 r3.add(rQuery.FieldByName('nickname').AsString);
                 rQuery.Next;
                end;
            result:=r1.Text+#13+r2.Text+#13+r3.text;
           tAProfile.SendString(tManager.RemoteProfiles.First,'List2',result);

        end;
end;
procedure TfAuth.tAProfileResourceReceived(const Sender: TObject;
  const AResource: TRemoteResource);
var i:integer;
r3,s1,s2,s3:TStringList;
begin


    if AResource.Hint='List2' then
      begin
        r3:=TStringList.Create;
        r3.Text:=AResource.Value.AsString;    // <-From Server

        s1:=TStringList.Create;
        s2:=TStringList.Create;
        s3:=TStringList.Create;

      s1.Text:=SplitString(r3.Text,#13)[0]; //Split username
      s2.Text:=SplitString(r3.Text,#13)[1]; //password
      s3.Text:=SplitString(r3.Text,#13)[2]; //nickname

       for i := 0 to r3.Count do
           begin
            StringGrid1.Cells[0,i]:=s1[i];   
            StringGrid1.Cells[1,i]:=s2[i];  
            StringGrid1.Cells[2,i]:=s3[i]; 
           end;
    end;

end;
 result:=r1.Text+#0+r2.Text+#0+r3.text;
 s1.Text:=SplitString(r3.Text,#0)[0];

  s2.Text:=SplitString(r3.Text,#0)[1];

  s3.Text:=SplitString(r3.Text,#0)[2];

我认为您应该编写
s1.Add(SplitString(r3.Text,#13)[0])
而不是
s1.Text:=SplitString(r3.Text,#13)[0]
`兄弟,在我的情况下如何使用JSON;对不起,Alex,这是一个太宽泛的主题,在这里无法回答堆栈溢出问题。要了解帮助和示例中的入门信息,请使用谷歌搜索“delphi json”等。网上有大量的文档和文章。