zoukankan      html  css  js  c++  java
  • Delphi中的InStrRev函数(倒找文本)

    写HTML解释部分的时候,需要在一个完整的URL中,提取出该URL的路径,如
    http://www.515800.cn/blog/index.asp
    需要提取出
    http://www.515800.cn/blog/
    这个路径出来,这个如果在VB/VBS中,使用InStrRev函数,很容易就可以实现,但在Delphi中,虽然经常用Pos来判断字符的的位置,但从来没有看到过PosRev之类的函数,查了帮助也没有便到。本来想自己写一个,但一想到网络这个宝库,还是先找找看再说,说不定有拿位前辈已经为我们写好了。
      驾着Google,搜了Delphi InStrRev,在第一页就找到了开发俱乐部(http://www.dev-club.com/)的一个精华贴(http://www.dev-club.com/club/bbs/showEssence.asp?id=14388),贴子中说
    "由于Delphi没有提供象VB中InstrRev()这样的函数.而Pos()是从字串左边开始查找的。",看来Delphi中真的没有这个相同功能的函数了,好在该贴子中有提供一个函数,正在使用该函数进,忽然想来Delphi中有一个函数ExtractFilePath跟我要的功能相似,于是立即用ExtractFilePath试了一下,得到的结果不是我要的。不过这没有让我灰心,习惯地查看了ExtractFilePath的函数源代码:

    function ExtractFilePath(const FileName: string): string;
    var
    I: Integer;
    begin
    I := LastDelimiter(PathDelim + DriveDelim, FileName);
    Result := Copy(FileName, 1, I);
    end;

    一眼就看到了LastDelimiter,踏破铁鞋无觅处,来得全不费功夫。

    终于用了简单的几句实现了getBaseURL函数了。


    function getBaseURL(sURL:string):string;
    begin
    if Pos('?',sURL)>0 then
    begin
    Result:=LeftStr(sURL,Pos('?',sURL)-1);
    end;
    Result := Copy(Result, 1, LastDelimiter('/', Result));
    end;


    LastDelimiter的实现:

    function LastDelimiter(const Delimiters, S: string): Integer;
    var
    P: PChar;
    begin
    Result := Length(S);
    P := PChar(Delimiters);
    while Result > 0 do
    begin
    if (S[Result] <> #0) and (StrScan(P, S[Result]) <> nil) then
    {$IFDEF MSWINDOWS}
    if (ByteType(S, Result) = mbTrailByte) then
    Dec(Result)
    else
    Exit;
    {$ENDIF}
    {$IFDEF LINUX}
    begin
    if (ByteType(S, Result) <> mbTrailByte) then
    Exit;
    Dec(Result);
    while ByteType(S, Result) = mbTrailByte do Dec(Result);
    end;
    {$ENDIF}
    Dec(Result);
    end;
    end;

  • 相关阅读:
    http://localhost:8080/ 演出Oracle说明
    JS浏览器类型推断方法
    MVC设计模式JavaWeb实现
    《TCP/IP详细说明》读书笔记(17章)-TCP传输控制协定
    创建表单
    道路软件质量:SourceMonitor
    HDU
    HDU 3032 Nim or not Nim? (sg函数求解)
    OpenWRT推理client线上的数
    IIS的ISAPI接口简介
  • 原文地址:https://www.cnblogs.com/hackpig/p/1668543.html
Copyright © 2011-2022 走看看