我正在尝试解析一个看起来与此类似的字符串(sys)
-1|low
0|normal
1|high
我需要在组合框中对它们进行配对,例如,low是标题,-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
答案 0 :(得分:3)
自己手动解析。
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]);