Update name value in TStringList name / value pair

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

List.Names[I]:= name; 

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

Or do I need to do a full update of the entire line

 List[I]:= name=value 

the problem is that I store a large number of string values ​​in the value part of the name / value pair

Example

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

I would rather just update the name part 9, because all I need to do)

thanks

+4
source share
1 answer

You need to read the index of the element representing the name / value pair. Do this by calling IndexOfName() . And then you change this item. Therefore, the code looks something like this:

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

Since you are on Delphi that supports generics, you might be better off with TDictionary<string, string> .

Even then, there is no trivial change in the name of the element. Translated into the dictionary, the code is as follows:

 Item := Dict.ExtractPair(OldName); Dict.Add(NewName, Item.Value); 
+7
source

All Articles