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;
CodePudding user response:
I figured it out, I have to use strict delimiter = true
and my string values should be separated not with ':'
but with '='
instead.
Delphi automatically recognizes equals signs!
Thanks.
CodePudding user response:
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;