Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating the name value in a TStringList name/value pair

Is it possible to update the Name string of a particular TStringList name/value pair?

List.Names[I]:= name;

I know Names is a readonly property, I was wondering if there was another way that I don't know about?

Or do I have to do a entire update of the entire string

List[I]:= name=value

the problem is I store a large amount of string values in the value portion of the name/value pair

example

name=value1,value2,value3,value4,value5,value6,value7,value8,value9,value10

I would much rather just update the name portion 9cause that's all I need to do)

thanks

like image 420
JakeSays Avatar asked Mar 27 '13 13:03

JakeSays


1 Answers

You need read the index of the item representing the name/value pair. Do that by calling IndexOfName(). And then you modify that item. So the code would look a little like this:

Index := List.IndexOfName(OldName);
if Index=-1 then
  // handle error
List[Index] := NewName + List.NameValueSeparator + List.ValueFromIndex[Index];

Since you are on a Delphi that has support for generics, you may be better off with TDictionary<string, string>.

Even then it's not trivial to change the name of an item. Translated to a dictionary the code looks like this:

Item := Dict.ExtractPair(OldName);
Dict.Add(NewName, Item.Value);
like image 102
David Heffernan Avatar answered Oct 26 '22 06:10

David Heffernan