Search code examples
delphidelimiterseparatortstringlist

Delphi: extract value from string with multiple separators and delimiters


I have a string like this:

;EncoderMin:250;EncoderMax:5755;MinPixel:-240;MaxPixel:980;

Given a function that returns a variant type and accepts as parameters the attribute type (For example EncoderMin), i want that the function returns the value of the attribute, so in this case 250

I am using this code but i cannot handle 2 delimiters. The first delimiter should be ';' that separates each attribute and the second delimiter should be ':' that separates the attribute from its value.

function TFrameLayout3DSTD.GetAttributiSTD(ALoc: Integer;
  AField: String; AVarType: Word): Variant;
var
  Q: TADOQuery;
  LLista : TStringList;
begin
  Result := null;

  Q := DMConn.GetQuery(
    'select AttributiSTD from Locazioni3D where idLocazione = %d', [ALoc]);
  try
    Q.Open;
    LLista := TStringList.Create;
    try
      LLista.Delimiter := ';';
      LLista.StrictDelimiter := True;
      LLista.DelimitedText := Q.Fields[0].AsString;

      Result := LLista.Values[AField];
    finally
      LLista.Free;
    end;
  finally
    Q.Free;
  end;
end;

Where Q.Fields[0].AsString is equals to ;EncoderMin:250;EncoderMax:5755;MinPixel:-240;MaxPixel:980;


Solution

  • I figured it out, the delimiter should be '=' instead of ':' .

    Delphi automatically recognizes equals signs!

    Thanks.