相关疑难解决方法(0)

如何循环分隔字符串并将字符串的内容分配给本地delphi变量?

我编写了一个Delphi函数,将.dat文件中的数据加载到字符串列表中.然后它解码字符串列表并分配给字符串变量.字符串的内容使用'#'符号作为分隔符.

然后,我如何获取此字符串的内容,然后将其内容分配给局部变量?

// Function loads data from a dat file and assigns to a String List.
function TfrmMain.LoadFromFile;
var 
  index, Count : integer;
  profileFile, DecodedString : string;
begin
  // Open a file and assign to a local variable.
  OpenDialog1.Execute;
  profileFile := OpenDialog1.FileName;
  if profileFile = '' then
    exit;
  profileList := TStringList.Create;
  profileList.LoadFromFile(profileFile);
  for index := 0 to profileList.Count - 1 do
  begin
    Line := '';
    Line := profileList[Index];
  end;
end;
Run Code Online (Sandbox Code Playgroud)

在解码后,var"Line"包含如下所示的内容:

例:

Line '23#80#10#2#1#...255#'.
Run Code Online (Sandbox Code Playgroud)

并非所有分隔符之间的值都是相同的长度,并且每次调用函数LoadFromFile时"Line"的值都会变化(例如,有时一个值可能只有一个数字,接下来的两个或三个等等,所以我不能依赖于复制字符串或数组的函数).

我试图找出循环"Line"内容的方法,将其分配给名为"buffer"的局部变量,然后如果遇到'#'则将缓冲区的值赋给局部变量,将缓冲区重新初始化为''; 然后移动到"Line"中的下一个值,重复下一个参数的过程,每次忽略'#'.

我想我现在已经解决了这个问题太久了,我似乎无法取得任何进展,需要休息一下.如果有人愿意看一看,我欢迎任何关于如何实现这一目标的建议.

非常感谢

KD

delphi loops tstringlist delphi-xe2

0
推荐指数
1
解决办法
2337
查看次数

标签 统计

delphi ×1

delphi-xe2 ×1

loops ×1

tstringlist ×1