zoukankan      html  css  js  c++  java
  • Delphi url 编码及转码及特殊字符串替换--百度和腾讯用的就是这个

    先介绍一下,Delphi中处理Google的URL编码解码,其中就会明白URL编码转换的方法的

    从delphi的角度看Google(谷歌)URL编码解码方式 
    在网上搜索了一下,似乎没有什么关于google的URL编码解码的很详细的资料,因此在这里写一下,希望给有用的人提供一点帮助。 
    使用google谷歌搜索一下关键词,对应的编码如下: 
    刀:%E5%88%80 
    刀具:%E5%88%80%E5%85%B7 
    刀具网:%E5%88%80%E5%85%B7%E7%9A%84 
    因此可见,google对URL的编码并非简单的httpencode编码,因为httpencode编码后的字符串应该是带有两个百分号的,而这个是三个百分号。多尝试一下就不难发现googleURL的编码解码规则如下:
    1、编码,先UTF8编码,然后再URL编码。 
          使用delphi编码的方法:加入待编码原始字符串为OiginStr,则编码后的字符串为: 
          NewStr:=HttpEncode(UTF8Encode(OiginStr)) 
          其中HttpEncode函数需要用到HttpApp,记得uses中加入。 
    2、解码,先URL解码,然后再UTF8解码。 
          使用delphi解码的方法:加入待解码字符串为NewStr,则解码后的原始字符串为: 
          OiginStr:=UTF8Decode(HttpDecode(NewStr)) 
          其中HttpEncode函数需要用到HttpApp,记得uses中加入。 
    相对来说百度就简单得多,一个httpencode就搞定了

    对于特殊字符如 *,空格等,Delphi会编译成* 和+,这样就不能通过URL编译,所以需要用到下面的函数。

    function StringReplace (const S, OldPattern, NewPattern: string; Flags: TReplaceFlags): string;

    rfReplaceAll:全部替换
    rfIgnoreCase:忽略大小写

    For Example:

    var
        aStr: String;
    begin
        aStr := 'This is a book, not a pen!';
        ShowMessage(StringReplace (aStr, 'a', 'two', []));//This is two book, not a pen!只替换了第一个符合的字
        ShowMessage(StringReplace (aStr, 'a', 'two', [rfReplaceAll]));//This is two book, not two pen!替换了所有符合的字
        aStr := 'This is a book, not A pen!';
        ShowMessage(StringReplace (aStr, 'a', 'two', [rfReplaceAll]));//This is two book, not A pen!只替换了符合的字(小写a)
        ShowMessage(StringReplace (aStr, 'a', 'two', [rfReplaceAll, rfIgnoreCase]));//This is two book, not two pen!不管大小写替换了所有符合的字
    end;

    这样,我们只有调用两次stringreplace函数就搞定了。

    http://blog.csdn.net/syndicater/article/details/17787065

  • 相关阅读:
    sqlite遇到database is locked问题的完美解决
    Delphi使程序的窗口出现在最前面并激活
    win10 家庭版不支持gpedit.msc的解决办法
    Delphi Record To Stream
    SQL Server 查看CPU情况
    JavaScript 获取 Url 上的参数(QueryString)值
    关于EF分页查询报错(Count must have a non-negative value.)的解决方案
    调用微信退款接口时出现System.Security.Cryptography.CryptographicException: 出现了内部错误 解决办法
    Javascript中“==”与“===”的区别
    IE浏览器各版本的CSS Hack
  • 原文地址:https://www.cnblogs.com/findumars/p/6358500.html
Copyright © 2011-2022 走看看