关于字符串处理

时间:2021-06-24 19:42:38
{"userId":156827617,"userName":"\u84bc\u51a5","headPic":"http:\/\/qlogo2.store.qq.com\/qzonelogo\/156300677\/1\/1252294148","yellowlevel":1,"yellowstatus":1,"exp":4659,"money":6308,"pf":0},

{"userId":403811,"userName":"paul","headPic":"http:\/\/imgcache.qq.com\/qzone_v4\/client\/userinfo_icon\/5001.gif","yellowlevel":0,"yellowstatus":0,"exp":4147,"money":1735,"pf":0},

{"userId":623150,"userName":"\u98d8\u96f6","headPic":"http:\/\/qlogo3.store.qq.com\/qzonelogo\/341862\/1\/1233044244","yellowlevel":4,"yellowstatus":2,"exp":35623,"money":187569,"pf":0}

上面一段字符串,有什么方法可以快速循环获取userId后面的数值并且很准确,谢谢

6 个解决方案

#1


x:=pos('"userId":',s);
delete(s,1,x+9);
x:=pos(',");
result:=copy(s,1,x-1);


一些异常判断自己加

#2


x:=pos(',"',s);
result:=copy(s,1,x-1);

#3


复杂字串处理建议用第三方开源的PerlRegExp控件,用正则表达式,功能强,官方下载
http://www.regular-expressions.info/delphi.html
使用方法,自己搜一下

#4


//uses ComObj
procedure TForm1.Button1Click(Sender: TObject);
var
  s: String;
  v, m: Variant;
  i: Integer;
begin
  s := '{"userId":156827617,"userName":"\u84bc\u51a5","headPic":"http:\/\/qlogo2.store.qq.com\/qzonelogo\/156300677\/1\/1252294148","yellowlevel":1,"yellowstatus":1,"exp":4659,"money":6308,"pf":0},' + #13#10

     + '{"userId":403811,"userName":"paul","headPic":"http:\/\/imgcache.qq.com\/qzone_v4\/client\/userinfo_icon\/5001.gif","yellowlevel":0,"yellowstatus":0,"exp":4147,"money":1735,"pf":0},' + #13#10

     + '{"userId":623150,"userName":"\u98d8\u96f6","headPic":"http:\/\/qlogo3.store.qq.com\/qzonelogo\/341862\/1\/1233044244","yellowlevel":4,"yellowstatus":2,"exp":35623,"money":187569,"pf":0} ';

  v := CreateOleObject('VBScript.RegExp');
  v.Pattern := '\{"userId":(\d+),';
  v.Global := True;
  v.Multiline := True;
  if v.Test(s) then
  begin
    m := v.Execute(s);
    for i := 0 to m.Count - 1 do
      ShowMessage(m.Item[i].SubMatches.Item[0])
  end;
end;

#5


这个麻烦

#6


搂主那个字符串不应该是分开的吧


procedure TForm1.Button1Click(Sender: TObject);
var
  s:string;
  l:TstringList;
  i:integer;
begin
  s:='{"userId":156827617,"userName":"\u84bc\u51a5","headPic":"http:\/\/qlogo2.store.qq.com\/qzonelogo\/156300677\/1\/1252294148","yellowlevel":1,"yellowstatus":1,"exp":4659,"money":6308,"pf":0},'+
     '{"userId":403811,"userName":"paul","headPic":"http:\/\/imgcache.qq.com\/qzone_v4\/client\/userinfo_icon\/5001.gif","yellowlevel":0,"yellowstatus":0,"exp":4147,"money":1735,"pf":0},'+
     '{"userId":623150,"userName":"\u98d8\u96f6","headPic":"http:\/\/qlogo3.store.qq.com\/qzonelogo\/341862\/1\/1233044244","yellowlevel":4,"yellowstatus":2,"exp":35623,"money":187569,"pf":0}';
  l:=TStringList.Create;
  l.Delimiter:=',';
  l.DelimitedText:=s;
  for i:=0 to l.Count-1 do
   if Pos('"userId":',l.Strings[i])<>0 then
     ShowMessage(Copy(l.Strings[i],Pos(':',l.Strings[i])+1,20));
  l.Free;
end;

#1


x:=pos('"userId":',s);
delete(s,1,x+9);
x:=pos(',");
result:=copy(s,1,x-1);


一些异常判断自己加

#2


x:=pos(',"',s);
result:=copy(s,1,x-1);

#3


复杂字串处理建议用第三方开源的PerlRegExp控件,用正则表达式,功能强,官方下载
http://www.regular-expressions.info/delphi.html
使用方法,自己搜一下

#4


//uses ComObj
procedure TForm1.Button1Click(Sender: TObject);
var
  s: String;
  v, m: Variant;
  i: Integer;
begin
  s := '{"userId":156827617,"userName":"\u84bc\u51a5","headPic":"http:\/\/qlogo2.store.qq.com\/qzonelogo\/156300677\/1\/1252294148","yellowlevel":1,"yellowstatus":1,"exp":4659,"money":6308,"pf":0},' + #13#10

     + '{"userId":403811,"userName":"paul","headPic":"http:\/\/imgcache.qq.com\/qzone_v4\/client\/userinfo_icon\/5001.gif","yellowlevel":0,"yellowstatus":0,"exp":4147,"money":1735,"pf":0},' + #13#10

     + '{"userId":623150,"userName":"\u98d8\u96f6","headPic":"http:\/\/qlogo3.store.qq.com\/qzonelogo\/341862\/1\/1233044244","yellowlevel":4,"yellowstatus":2,"exp":35623,"money":187569,"pf":0} ';

  v := CreateOleObject('VBScript.RegExp');
  v.Pattern := '\{"userId":(\d+),';
  v.Global := True;
  v.Multiline := True;
  if v.Test(s) then
  begin
    m := v.Execute(s);
    for i := 0 to m.Count - 1 do
      ShowMessage(m.Item[i].SubMatches.Item[0])
  end;
end;

#5


这个麻烦

#6


搂主那个字符串不应该是分开的吧


procedure TForm1.Button1Click(Sender: TObject);
var
  s:string;
  l:TstringList;
  i:integer;
begin
  s:='{"userId":156827617,"userName":"\u84bc\u51a5","headPic":"http:\/\/qlogo2.store.qq.com\/qzonelogo\/156300677\/1\/1252294148","yellowlevel":1,"yellowstatus":1,"exp":4659,"money":6308,"pf":0},'+
     '{"userId":403811,"userName":"paul","headPic":"http:\/\/imgcache.qq.com\/qzone_v4\/client\/userinfo_icon\/5001.gif","yellowlevel":0,"yellowstatus":0,"exp":4147,"money":1735,"pf":0},'+
     '{"userId":623150,"userName":"\u98d8\u96f6","headPic":"http:\/\/qlogo3.store.qq.com\/qzonelogo\/341862\/1\/1233044244","yellowlevel":4,"yellowstatus":2,"exp":35623,"money":187569,"pf":0}';
  l:=TStringList.Create;
  l.Delimiter:=',';
  l.DelimitedText:=s;
  for i:=0 to l.Count-1 do
   if Pos('"userId":',l.Strings[i])<>0 then
     ShowMessage(Copy(l.Strings[i],Pos(':',l.Strings[i])+1,20));
  l.Free;
end;