function GetItemValueBySplitString(sSource:string; sItemName:string; cSplitChar1:char; cSplitChar2:char):string;
var
i, iPos: integer;
sSrc, sSubItem, sName: string;
cChar1, cChar2: char;
ResultList: TStringList;
begin
sSrc := Trim(sSource);
sName := Trim(sItemName);
cChar1 := cSplitChar1;
cChar2 := cSplitChar2;
if (Length(sSrc)=0) or (Length(sName)=0) then
begin
Result := '';
Exit;
end;
if pos(cChar1, sSrc) =0 then
begin
Result := '';
Exit;
end;
i := 0; iPos := 0;
ResultList := TStringList.Create;
try
ResultList.Delimiter := cChar1;
ResultList.DelimitedText := sSrc;
if ResultList.Count > 0 then
begin
for i:= 0 to ResultList.Count-1 do
begin
sSubItem := ResultList.Strings[i];
iPos := pos(cChar2, sSubItem);
if iPos > 0 then
begin
if Copy(sSubItem, 1, iPos -1) = sItemName then
begin
Result := Trim(Copy(sSubItem, iPos + 1, Length(sSubItem)));
Exit;
end;
end;
end;
end;
finally
FreeAndNil(ResultList);
end;
end;
//sample:
//var
// s1: string;
//begin
// s1 := 'USER_CODE:8888, USER_ROLE:2';
// ShowMessage(GetItemValueBySplitString(s1, 'USER_CODE', ',', ':'));
有时候可能会用的到,先贴在这。