0

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;

1
  • 2
    Just add a LLista.NameValueSeparator := ':'; Commented Sep 30, 2021 at 12:56

2 Answers 2

2

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

Delphi automatically recognizes equals signs!

Thanks.

Sign up to request clarification or add additional context in comments.

1 Comment

I don't understand: you already set .StrictDelimiter := True in your question.
0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.