Stringlist作为字符串分隔符? [英] Stringlist with delimiter as a string?

查看:208
本文介绍了Stringlist作为字符串分隔符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个存储为字符串的对象中有一个名为HistoryText的属性。
我想显示网格中的所有行。我应该能够删除和编辑网格中的行。
格式为:

I have an attribute called HistoryText in a object that is stored as a string. I want to show all rows in a grid. I should be able to delete and edit rows in the grid. The format is:

16.5.2003-$-12:09-$-anna-$-Organization created
2.6.2005-$-13:03-$-jimmy-$-Organization edited
19.12.2005-$-13:33-$-madeleine-$-Organization edited

所以每一行都有4个字段,日期,时间,用户和消息,分隔符字符串为 - $ - 。
作为分隔符,一个字符串而不是一个字符,它不能分配给stringlists delimiter属性。

So each row have 4 fields, date, time, user, and message with a delimiter string as '-$-'. As the delimiter a string and not a char it cannot be assigned to the stringlists delimiter property.

我有一个例程将字符串提取到Stringlist:

I have a routine to extract the string to a Stringlist:

procedure ParseDelimited(const aStringList: TStringList; const aOrgList, aDelimiter: string);
var
   vDelimiterPos : integer;
   vPartialStr : string;
   vRemaingTxt : string;
   vDelimiterLength : integer;
begin
   vDelimiterLength := Length(aDelimiter);

   if (AnsiRightStr(aOrgList, Length(aDelimiter)) = aDelimiter) then
     vRemaingTxt := aOrgList
   else
     vRemaingTxt := aOrgList + aDelimiter;

   aStringList.BeginUpdate;
   aStringList.Clear;
   try
     while Length(vRemaingTxt) > 0 do
     begin
       vDelimiterPos := Pos(aDelimiter, vRemaingTxt);
       vPartialStr := Copy(vRemaingTxt,0,vDelimiterPos-1);
       aStringList.Add(vPartialStr);
       vRemaingTxt := Copy(vRemaingTxt,vDelimiterPos+vDelimiterLength,MaxInt);
     end;
   finally
     aStringList.EndUpdate;
   end;
end;

它似乎工作正常。我的问题是将StringList中的更改同步到原始的String属性?这个分隔符有太多的历史数据,所以我不认为把它改成TChar是一个现实的选择。

and it seems to work fine. My problem is syncing the changes in the StringList back to the original String property ? There are so much historical data with this delimiter so I don't think change it to a TChar is a realistic option.

更新:
澄清。我想我可以使用上面的方法将String转换为StringList。然后在网格中显示它不应该那么难。当我想将TStringList转换回原始String属性wih' - $ - '作为分隔符时,问题出现。我不能做HistoryText:= myStringList.Delimitedtext例如。

Update: A clarification. I think I can manage to convert the String to a StringList with the method above. Then display it in the grid should not be so hard. The problem come when I want to convert the TStringList back to the original String property wih '-$-' as delimiter. I cannot do HistoryText := myStringList.Delimitedtext for example.

第二次更新:
我已经解决了。你们都得到了一个+1的快速答案,真的试图帮助。总结如何做到这一点。

Second update: I have solved it. You all got a +1 for fast answers and really trying to help. In summary how I did it.

从历史文本阅读:

MyStringList.Text := Historytext;

现在每行都有3个分隔符为' - $ - ',每一行都用换行符分隔

Now each row have 3 delimiters of '-$-' and each line is separated by a linefeed as usual.


  • 在循环中解析Stringlist并将其显示在网格中。我不用担心MyStringList了。

  • 让用户删除和编辑网格中的行。

  • 当行和列逐行循环完成时网格并建立与原始格式相同的新字符串。

  • 将该字符串分配给HistoryText。

  • In a loop parse the Stringlist and show it in the grid. I don't bother about MyStringList anymore.
  • Let the user delete and edit rows in the grid.
  • When finished loop by row and columns in the grid and build a new string with the same format as original.
  • Assign that string to HistoryText.

所以将焦点从StringList转移到网格使它更容易:)

So shift focus from StringList to the grid made it easier :)

推荐答案

而不是分隔符(char)和DelimitedText,你也可以使用LineBreak(一个字符串)和Text:

Instead of Delimiter (a char) and DelimitedText, you can also use LineBreak (a string) and Text:

lst := TStringList.Create;
try
  lst.LineBreak := '-$-';
  lst.Text := '16.5.2003-$-12:09-$-anna-$-Organization created';
  Memo1.Lines := lst;  // or whatever you do with it
finally
  lst.Free;
end;

即使是其他方式,也可以使用它。

Ans it works even the other way round.

这篇关于Stringlist作为字符串分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆