quotedstr(Delphi中谨慎使用QuotedStr、QuotedString、DequotedString相关的函数[通俗易懂])

发布时间:2025-12-10 20:07:30 浏览次数:2

Delphi中谨慎使用QuotedStr、QuotedString、DequotedString相关的函数[通俗易懂]-

Delphi中谨慎使用QuotedStr、QuotedString、DequotedString相关的函数[通俗易懂]//以下测试代码vars,ss:string;begin//1.添加定界符(内容中含定界符的要转义)ss:=QuotedStr(s);//单引号ss:=s.QuotedString(””);//单引号//2.添加指定的定界符(内容中含定界符的要转义)ss:=AnsiQuotedStr(s,'”‘);//双引号ss:=s.QuotedString(‘”‘);//双引号//3.去掉定界符(内容中含连续两个定界符的要反转义)…_delphiquoted

//以下测试代码var  s, ss: string;begin  //JSON中包含大量双引号,方便测试出效果  with TStreamReader.Create('全国adcode.json', TEncoding.UTF8) do  try    s := ReadToEnd;  finally    Free;  end;  //1.添加定界符(内容中含定界符的要转义)  ss := QuotedStr(s);//单引号  ss := s.QuotedString('''');//单引号  //2.添加指定的定界符(内容中含定界符的要转义)  ss :=AnsiQuotedStr(s, '"');//双引号  ss :=s.QuotedString('"');//双引号  //3.去掉定界符(内容中含连续两个定界符的要反转义)  ss := AnsiDequotedStr(s, '"');//双引号  ss := s.DeQuotedString('"');//双引号end;//经测试发现在个别情况下StringHelper方法非常慢//1.s中含有大量双引号时,添加双引号定界符//ss :=s.QuotedString('"');//非常慢,比ss :=AnsiQuotedStr(s, '"')慢几十倍//2.s中含有大量双引号转义时,去除双引号定界符//ss := s.DeQuotedString('"');//非常慢,比ss := AnsiDequotedStr(s, '"')慢几十倍//3.单引号的添加与去除也是一样的//所以,使用StringHelper的QuotedString和DeQuotedString方法时,要注意//通过查看源码,发现AnsiQuotedStr和AnsiDequotedStr使用的指针操作//而StringHelper的QuotedString和DeQuotedString使用逐个字符拷贝

希望我今天分享的这篇文章可以帮到您。

需要做网站?需要网络推广?欢迎咨询客户经理 13272073477