'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 1:[1]

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

Delphi automatically recognizes equals signs!

Thanks.

Solution 2:[2]

Another alternative is Split and Indextext.

uses
   System.SysUtils, System.StrUtils, System.Variants;
var
  txt: string;
  arr: TArray<string>;
  i  : integer;
  v  : variant;
begin
  try
    v  := null;
    txt := ';z:250;a:17;EncoderMin:250;EncoderMax:5755;MinPixel:-240;MaxPixel:980;';
    arr := txt.Split([';', ':']);

    i  := IndexText('EncoderMin', arr);
    if i >= 0 then
      v := arr[succ(i)];
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 USauter