Question

I have a TStringList that is loaded with a few thousand Name-Value Pairs. One of them is 004001000002000=Timbre2 Volume 0~127 4995

I find a specific index by calling IndexOfName with the string '004001000002000'. I expect ValueFromIndex to return the string 'Timbre2 Volume 0~127 4995'

Instead, when I access this value using the ValueFromIndex, it returns the string:

~127 4995

What causes this? Is Tilde a special character that causes the string to be truncated? Can I set it to something else?

Was it helpful?

Solution

I can't reproduce the problem using the following code in a TButton.OnClick event (Delphi 2007 and Delphi 7 - screen capture from Delphi 2007 test):

procedure TForm2.Button2Click(Sender: TObject);
var
  SL: TStringList;
  i: Integer;
begin
  SL := TStringList.Create;
  try
    SL.Add('004001000002000=Timbre2 Volume 0~127 4995');
    SL.Add('ABCDEF=Testing 1 2 3');
    i := SL.IndexOfName('004001000002000');
    if i > -1 then
      ShowMessage(SL.ValueFromIndex[i])
    else
      ShowMessage('IndexOfName returned -1');
  finally
    SL.Free;
  end;
end;

This correctly shows the expected dialog:

ShowMessage result

I also tested using the simpler method:

    ShowMessage(SL.Values['004001000002000']);

This displayed the identical ShowMessage dialog.

OTHER TIPS

Tilde is not a special character to TStringList, unless you explicitally define it as one.

TStringList.IndexOfName() and TStringList.Name[Index] look only at what is in front of the first NameValueSeparator character, and TStringList.ValueFromIndex[Index] returns everything that is after the first NameValueSeparator character, the value is not truncated in any way. TStringList.NameValueSeparator is set to '=' by default. So if you are seeing truncation occuring, then either you are truncating it in your own code, or the TStringList contains a line that actually says '004001000002000=~127 4995'.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top