Delphi   发布时间:2022-04-11  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了delphi – 将字符串复制到StringGrid?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想将备忘录的内容复制到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

大佬总结

以上是大佬教程为你收集整理的delphi – 将字符串复制到StringGrid?全部内容,希望文章能够帮你解决delphi – 将字符串复制到StringGrid?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。