加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 将字符串复制到StringGrid?

发布时间:2020-12-15 04:30:03 所属栏目:大数据 来源:网络整理
导读:我想将备忘录的内容复制到TStringGrid. 如果字符串之间存在空格或间隙,则应将该字添加到StringGrid中的单元格中. 所以,假设我的Wordwrapped Memo包含一些信息: 我怎么能将这些信息复制到StringGrid? 出于这个例子的目的,我制作了一个示例Image来说明结果应
我想将备忘录的内容复制到TStringGrid.

如果字符串之间存在空格或间隙,则应将该字添加到StringGrid中的单元格中.

所以,假设我的Wordwrapped Memo包含一些信息:

我怎么能将这些信息复制到StringGrid?

出于这个例子的目的,我制作了一个示例Image来说明结果应该如何:

重要的是要知道我不会总是知道要使用的列数,例如,如果从文本文件加载备忘录.

也许预定数量的列会更好,例如5或6列.行数也将是未知的.

我怎么能这样做?

解决方法

如果我找对你,那么应该这样做:
procedure TForm1.FormClick(Sender: TObject);
type
  TWordPos = record
    Start,&End: integer;
  end;
const
  ALLOC_BY = 1024;
var
  Words: array of TWordPos;
  ActualLength,i: integer;
  txt: string;
  ThisWhite,PrevWhite: boolean;
begin

  ActualLength := 0;
  txt := Memo1.Text;
  PrevWhite := true;
  for i := 1 to Length(txt) do
  begin
    ThisWhite := Character.IsWhiteSpace(txt[i]);
    if PrevWhite and not ThisWhite then
    begin
      if ActualLength = Length(Words) then
        SetLength(Words,Length(Words) + ALLOC_BY);
      Words[ActualLength].Start := i;
      inc(ActualLength);
      PrevWhite := false;
    end else if (ActualLength>0) and ThisWhite then
      Words[ActualLength - 1].&End := i;
    PrevWhite := ThisWhite;
  end;

  SetLength(Words,ActualLength);

  StringGrid1.RowCount := Ceil(Length(Words) / StringGrid1.ColCount);

  for i := 0 to Length(Words) - 1 do
  begin
    StringGrid1.Cells[i mod StringGrid1.ColCount,i div StringGrid1.ColCount] :=
      Copy(Memo1.Text,Words[i].Start,Words[i].&End - Words[i].Start);
  end;

end;

Screenshot http://privat.rejbrand.se/stringgridwordsfrommemo.png

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读