Author: Jonas Bilinkevicius
I have a TStringList that looks something like:
a = one
b = two
c = three
c = four
Could someone please tell me to access both values of c? When I try to use
(StringList.Values['c']) to get the value of c, I can only ever get the first
value, and I need both. Even looping through every item in the list would be fine,
but I can't get that to work either. StringList[I] in a for loop only gives me the
whole line, like c=three, and I just want the value three.
Answer:
Solve 1:
1
2 procedure GetMatchingValues(strKey: string; slSource, slResult: TStringList);
3 var
4 i, nStart: integer;
5 begin
6 slResult.Clear;
7 strKey := strKey + '=';
8 nStart := Length(strKey) + 1;
9 for i := 0 to slSource.Count - 1 do
10 begin
11 if Pos(strKey, slSource[i]) = 1 then
12 slResult.Add(Copy(slSource[i], nStart, Length(slSource[i])));
13 end;
14 end;
Solve 2:
Try something like the following:
15 for i := 0 to SL.Count - 1 do
16 begin
17 temp := SL[i];
18 p := pos('=', temp);
19 key := copy(temp, 1, p - 1);
20 if key = 'c' then
21 value := copy(temp, p + 1, length(temp));
22 end;
|