带有分隔符作为字符串的字符串列表?

发布于 2024-09-10 14:39:38 字数 1887 浏览 12 评论 0原文

我在一个存储为字符串的对象中有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

此岸叶落 2024-09-17 14:39:38

除了 Delimiter(字符)和 DelimitedText,您还可以使用 LineBreak(字符串)和 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,甚至反之亦然。

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.

只涨不跌 2024-09-17 14:39:38

在黑暗中狂野刺伤(不太清楚你在问什么):

遍历网格行:

  • 对于每一行:
    • 将空字符串分配给临时字符串var
    • 对于每一列,将行/列值加上分隔符添加到临时字符串 var
    • 从临时字符串 var 中删除最后一个分隔符(如果它非空)
    • 将临时字符串 var 添加到字符串列表
  • Write stringlist 的 text 属性返回到您的 HistoryText

const
  Delimiter = '-$-';
var
  row: Integer;
  col: Integer;
  SL: TStringList;
  rowString: string;
begin
  SL := TStringList.Create;
  try
    for row := 0 to StringGrid1.RowCount - 1 do begin
      rowString := '';
      for col := 0 to StringGrid1.ColCount - 1 do begin
        rowString := StringGrid1.Cells[col, row] + Delimiter;
      end;
      if rowString <> '' then begin
        rowString := Copy(rowString, 1, Length(rowString) - Length(Delimiter));
      end;
      SL.Add(rowString);
    end;
    HistoryText := SL.Text;
  finally
    SL.Free;
  end;
end;

使用 Uwe 对 TStrings 的 LineBreak 属性的解决方案:

var
  row: Integer;
  col: Integer;
  SLRows: TStringList;
  SLCols: TStringlist;
begin
  SLRows := TStringList.Create;
  try
    SLCols := TStringList.Create;
    try
      SLCols.LineBreak := '-$-';
      for row := 0 to StringGrid1.RowCount - 1 do begin
        SLCols.Clear;
        for col := 0 to StringGrid1.ColCount - 1 do begin
          SLCols.Add(StringGrid1.Cells[col, row]);
        end;
        SLRows.Add(SLCols.Text);
      end;
      HistoryText := SLRows.Text;
    finally
      SLCols.Free;
    end;
  finally
    SLRows.Free;
  end;
end;

Wild stab in the dark (it isn't very clear what you are asking):

Work through the grid rows:

  • For each row:
    • assign an empty string to a temporary string var
    • For each column add row/column value plus your delimiter to temporary string var
    • remove last delimiter from temporary string var (if it is non-empty)
    • add temporary string var to stringlist
  • Write stringlist's text property back to your HistoryText

const
  Delimiter = '-$-';
var
  row: Integer;
  col: Integer;
  SL: TStringList;
  rowString: string;
begin
  SL := TStringList.Create;
  try
    for row := 0 to StringGrid1.RowCount - 1 do begin
      rowString := '';
      for col := 0 to StringGrid1.ColCount - 1 do begin
        rowString := StringGrid1.Cells[col, row] + Delimiter;
      end;
      if rowString <> '' then begin
        rowString := Copy(rowString, 1, Length(rowString) - Length(Delimiter));
      end;
      SL.Add(rowString);
    end;
    HistoryText := SL.Text;
  finally
    SL.Free;
  end;
end;

Using Uwe's solution of TStrings' LineBreak property:

var
  row: Integer;
  col: Integer;
  SLRows: TStringList;
  SLCols: TStringlist;
begin
  SLRows := TStringList.Create;
  try
    SLCols := TStringList.Create;
    try
      SLCols.LineBreak := '-$-';
      for row := 0 to StringGrid1.RowCount - 1 do begin
        SLCols.Clear;
        for col := 0 to StringGrid1.ColCount - 1 do begin
          SLCols.Add(StringGrid1.Cells[col, row]);
        end;
        SLRows.Add(SLCols.Text);
      end;
      HistoryText := SLRows.Text;
    finally
      SLCols.Free;
    end;
  finally
    SLRows.Free;
  end;
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文