如何在字符串中存储和加载键值对列表? [英] How do I store and load a list of key-value pairs in a string?

查看:275
本文介绍了如何在字符串中存储和加载键值对列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串列表以及它们将被替换的值.我正在尝试将它们合并到'O'='0',' .'='.', ...之类的列表中,因此我很容易对其进行编辑并添加更多替换对.

I have a list of strings and the values they are to be replaced with. I'm trying to combine them in a list like 'O'='0',' .'='.', ... so it's easy for me to edit it and add more pairs of replacements to make.

现在我能想到的最好的方法是:

Right now the best way I could think of it is:

var
  ListaLimpeza : TStringList;
begin
  ListaLimpeza := TStringList.Create;

  ListaLimpeza.Delimiter := '|';
  ListaLimpeza.QuoteChar := '"';
  ListaLimpeza.DelimitedText := 'O=0 | " .=."';

  ShowMessage('1o Valor = '+ListaLimpeza.Names[1]+' e 2o Valor = '+ListaLimpeza.ValueFromIndex[1]);

这行得通,但是对视觉效果不好,因为我不能像这样(对于空格字符来说非常直观)对before字符串(例如ex ' .')进行编码,所以只能像(" .)这样=可以在TStringList中分配名称和值.

This works, but it's not good for visuals, since I can't code the before string (for ex ' .') like that (which is very visual for the SPACE character), only like (" .) so that the = works to assign a name and value in the TStringList.

推荐答案

默认情况下,NamesValues必须采用Windows INI文件样式的=分隔. 不能通过AFAICT更改该分隔符.正如@SirRufo在评论中指出的(我从未注意到),您可以使用TStringList.NameValueSeparator属性进行更改.

The Names and Values by default have to be separated by =, in the style of Windows INI files. There's no way AFAICT to change that separator. As @SirRufo indicates in the comment (and which I had never noticed), you can change that using the TStringList.NameValueSeparator property.

这将使您了解Delphi认为的TStringList中的内容,而不是您认为的:

This will give you an idea of what Delphi thinks is in your TStringList, which is not what you think it is:

procedure TForm1.FormCreate(Sender: TObject);
var
  SL: TStringList;
  Temp: string;
  i: Integer;
begin
  SL := TStringList.Create;
  SL.Delimiter := '|';
  SL.QuoteChar := '"';
  SL.StrictDelimiter := True;
  SL.DelimitedText := 'O=0 | ! .!=!.!';
  Temp := 'Count: ' + IntToStr(SL.Count) + #13;
  for i := 0 to SL.Count - 1 do
    Temp := Temp + Format('Name: %s Value: %s'#13, 
              [SL.Names[i], SL.ValueFromIndex[i]]);
  ShowMessage(Temp);
end;

这将产生以下输出:

TStringList名称/值可能无法满足您的需求.目前尚不清楚您的实际目标是什么,但是看来一个带有text|replacement简单列表并对该文件进行纯解析的简单文本文件是否可以工作,并且您可以轻松地使用TStringList对该文件进行读取/写入,但是我看不到有任何方法可以轻松地进行解析,除非您自己进行解析.您可以在解析对时使用数组存储对:

TStringList Names/Values probably isn't going to do what you need. It's not clear what your actual goal is, but it appears that a simple text file with a simple list of text|replacement and plain parsing of that file would work, and you can easily use TStringList to read/write from that file, but I don't see any way to do the parsing easily except to do it yourself. You could use an array to store the pairs when you parse them:

type
  TReplacePair = record
    TextValue: string;
    ReplaceValue: string;
  end;

  TReplacePairs = array of TReplacePair;

function GetReplacementPairs: TReplacePairs;
var
  ConfigInfo: TStringList;
  i, Split: Integer;
begin
  ConfigInfo := TStringList.Create;
  try
    ConfigInfo.LoadFromFile('ReplacementPairs.txt');
    SetLength(Result, ConfigInfo.Count);
    for i := 0 to ConfigInfo.Count - 1 do
    begin
      Split := Pos('|`, ConfigInfo[i];
      Result[i].TextValue := Copy(ConfigInfo[i], 1, Split - 1);
      Result[i].ReplaceValue := Copy(ConfigInfo[i], Split + 1, MaxInt);
    end;
  finally
    ConfigInfo.Free;
  end;
end;

然后,您可以填充编辑/添加/删除替换对所需的任何控件,只需反转读取操作即可将它们写回保存.

You can then populate whatever controls you need to edit/add/delete the replacement pairs, and just reverse the read operation to write them back out to save.

这篇关于如何在字符串中存储和加载键值对列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆