带有分隔符作为字符串的字符串列表?
我在一个存储为字符串的对象中有一个名为 HistoryText 的属性。 我想显示网格中的所有行。我应该能够删除和编辑网格中的行。 格式为:
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 属性。
我有一个将字符串提取到字符串列表的例程:
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 不是一个现实的选择。
更新: 一个澄清。我想我可以用上面的方法将字符串转换为字符串列表。然后将其显示在网格中应该不会那么难。当我想将 TStringList 转换回原始 String 属性并以“-$-”作为分隔符时,问题就出现了。例如,我无法执行 HistoryText := myStringList.Delimitedtext 。
第二次更新: 我已经解决了。你们都因快速回答并真正尽力提供帮助而获得+1。总结一下我是如何做到的。
从历史文本中读取:
MyStringList.Text := Historytext;
现在每行都有 3 个分隔符“-$-”,并且每行像往常一样由换行符分隔。
- 在循环中解析字符串列表并将其显示在网格中。我不再关心 MyStringList 了。
- 让用户删除和编辑网格中的行。
- 完成后,在网格中按行和列循环,并构建一个与原始格式相同的新字符串。
- 将该字符串分配给 HistoryText。
因此,将焦点从 StringList 转移到网格会更容易:)
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
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.
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;
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.
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.
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.
Read from Historytext:
MyStringList.Text := Historytext;
Now each row have 3 delimiters of '-$-' and each line is separated by a linefeed as usual.
- 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.
So shift focus from StringList to the grid made it easier :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了 Delimiter(字符)和 DelimitedText,您还可以使用 LineBreak(字符串)和 Text:
Ans,甚至反之亦然。
Instead of Delimiter (a char) and DelimitedText, you can also use LineBreak (a string) and Text:
Ans it works even the other way round.
在黑暗中狂野刺伤(不太清楚你在问什么):
遍历网格行:
使用 Uwe 对 TStrings 的 LineBreak 属性的解决方案:
Wild stab in the dark (it isn't very clear what you are asking):
Work through the grid rows:
Using Uwe's solution of TStrings' LineBreak property: