【问题标题】:Delphi/pascal Parse a string to a comboboxDelphi/pascal 将字符串解析为组合框
【发布时间】:2013-01-28 10:50:56
【问题描述】:

我正在尝试解析一个看起来完全像这样的字符串 (sys)

-1|low
0|normal
1|high

我需要在一个组合框中将它们配对,例如,低是标题,-1 是值。做这个的最好方式是什么?到目前为止我所拥有的是:

 var
 sys : String;
 InputLine : TStringList;

   InputLine := TStringList.Create;
   InputLine.Delimiter := '|';
   InputLine.DelimitedText := sys;
   Combobox1.items.AddStrings(InputLine);
   FreeAndNil(InputLine)

这给出了组合框的每一行:

 -1
 low
 0
 normal
 1
 high

【问题讨论】:

  • 将您的字符串列表放入 for 并使用 for 循环的索引的奇数/偶数值形成您的组合
  • 我看不出你是如何挣扎的。您已经拆分了这些值。您需要做的就是将它们添加到组合框中。

标签: delphi delphi-2010


【解决方案1】:

自己手动解析。

var
  SL: TStringList;
  StrVal: string;
  IntVal: Integer;
  Line: string;
  DividerPos: Integer;
begin
  SL := TStringList.Create;
  try
    SL.LoadFromFile('Whatever.txt');
    for Line in SL do
    begin
      DividerPos := Pos('|', Line);
      if DividerPos > 0 then
      begin
        StrVal := Copy(Line, DividerPos + 1, Length(Line));
        IntVal := StrToInt(Copy(Line, 1, DividerPos - 1));
        ComboBox1.Items.AddObject(StrVal, TObject(IntVal));
      end;
    end
  finally
    SL.Free;
  end;
end;

从选定项中检索值:

if (ComboBox1.ItemIndex <> -1) then
  SelVal := Integer(ComboBox1.Items.Objects[ComboBox1.ItemIndex]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-02
    • 1970-01-01
    • 1970-01-01
    • 2015-02-15
    • 1970-01-01
    • 1970-01-01
    • 2012-10-14
    • 2017-04-04
    相关资源
    最近更新 更多