1.       提取字符串中指定子字符串前的字符串

  Function Before( Src:string ; S:string ): string ;

  Var

    F: Word ;

  begin

    F:= POS(Src,S) ;

    if F=0 then

      Before := S

     else

      Before := COPY(S,1,F-1) ;

  end ;

  eg: Before('123','helloworld_123')  返回结果:helloworld_

  2.       提取字符串中指定子字符串后的字符串

  function After(Src: string; S: string):string;

  var

    F: Word;

  begin

    F:= Pos(Src, S);

    if F = 0 then

      After:= ''

    else

      After:= Copy(S, F+Length(Src), Length(S));

  end;

  3.       Delphi 替换函数

  procedure Replace(var s:string;const SourceChar:pchar;const RChar:pchar);

  //第一个参数是原串,第二个是模式串,第三个是替换串

  var

    ta,i,j:integer;

    m,n,pn,sn:integer;

    SLen,SCLen,RCLen:integer;//SLen表示原串的长度,SCLen表示模式传的长度,RCLen表示替换串的长度

    IsSame:integer;

    newp:array of char;//用来保存替换后的字符数组

  begin

    SLen:=strlen(pchar(s));SCLen:=strlen(SourceChar);RCLen:=strlen(RChar);

    j:=pos(string(SourceChar),s);

    s:=s+chr(0);ta:=0;i:=j;

    while s[i]<>chr(0) do //这个循环用ta统计模式串在原串中出现的次数

    begin

      n:=0;IsSame:=1;

    for m:=i to i+SCLen-1 do

      begin

        if m>SLen then begin

          IsSame:=0;break;

        end;

        if s[m]<>sourceChar[n] then begin

          IsSame:=0;break;

        end;

        n:=n+1;

      end;

      if IsSame=1 then begin

        ta:=ta+1;i:=m;

      end

      else

        i:=i+1;

    end;

    if j>0 then

    begin

      pn:=0;sn:=1;

      setlength(newp,SLen-ta*SCLen+ta*RCLen+1);//分配newp的长度,+1表示后面还有一个#0结束符

      while s[sn]<>chr(0) do //主要循环,开始替换

      begin

        n:=0;IsSame:=1;

        for m:=sn to sn+SCLen-1 do //比较子串是否和模式串相同

        begin

          if m>SLen then begin IsSame:=0;break; end;

          if s[m]<>sourceChar[n] then begin IsSame:=0;break; end;

          n:=n+1;

        end;

        if IsSame=1 then//相同

        begin

          for m:=0 to RCLen-1 do

          begin

            newp[pn]:=RChar[m];pn:=pn+1;

          end;

          sn:=sn+SCLen;

        end

        else

        begin //不同

          newp[pn]:=s[sn];

          pn:=pn+1;sn:=sn+1;

        end;

      end;

      newp[pn]:=#0;

      s:=string(newp); //重置s,替换完成!

    end;

  end;

  4.       Delphi StringReplace() 替换字符串的用法

  str:= '{"UserName":"helloworld","UserPass":"helloworld_123","UserEmail":"lovecode@163.com"}';

  str:= StringReplace(str,'"','\"',[rfReplaceAll]);

  StringReplace(源字符串,'被替换字符','替换后字符',[rfReplaceAll]);

  5.       查找字符串中指定字符及字符串最后一次出现的位置

  function RightPosEx(const Substr,S: string): Integer;

  var

    iPos: Integer;

    TmpStr: string;

    i,j,len: Integer;

    PCharS,PCharSub: PChar;

  begin

    PCharS:=PChar(s); //将字符串转化为PChar格式 

    PCharSub:=PChar(Substr);

    Result:=0;

    len:=length(Substr);

    for i:=0 to length(S)-1 do begin

      for j:=0 to len-1 do begin

        if PCharS[i+j]<>PCharSub[j] then break;

      end;

      if j=len then Result:=i+1;

    end;

  end;

  调用方式:RightPosEx(‘\’,’123456\7\’);

delphi 字符串查找替换函数 转的更多相关文章

  1. java中String字符串的替换函数:replace与replaceAll的区别

    例如有如下x的字符串 String x = "[kllkklk\\kk\\kllkk]";要将里面的“kk”替换为++,可以使用两种方法得到相同的结果 replace(CharSe ...

  2. PHP 字符串正则替换函数preg_replace使用说明

    1. preg_replace() $msg = preg_replace("/<style>.+<\/style>/is", "", ...

  3. php字符串常用处理函数(数组的拆分、查找替换)

    //字符串常用函数    $a = "hello";    echo strlen($a); //输出字符串的长度        $b = "Hello";   ...

  4. Oracle内置函数:时间函数,转换函数,字符串函数,数值函数,替换函数

    dual单行单列的隐藏表,看不见 但是可以用,经常用来调内置函数.不用新建表 时间函数 sysdate 系统当前时间 add_months 作用:对日期的月份进行加减 写法:add_months(日期 ...

  5. php中几个字符串替换函数详解

    在php中字符替换函数有几个如有:str_replace.substr_replace.preg_replace.preg_split.str_split等函数,下面我来给大家总结介绍介绍. 一.st ...

  6. JavaScript字符串插入、删除、替换函数

    JavaScript字符串插入.删除.替换函数 说明: 以下函数中前两个函数取出查找字符串的前一部分和后一部分,以用于其他函数.注意,调用一次 replaceString(mainStr,search ...

  7. delphi字符串函数大全

    转帖:delphi字符串函数大全 2009-11-17 16:43:55 分类: delphi字符串函数大全 ━━━━━━━━━━━━━━━━━━━━━首部 function StringToGUID ...

  8. Delphi字符串处理函数

    1.Copy 功能说明:该函数用于从字符串中复制指定范围中的字符.该函数有3个参数.第一个参数是数据源(即被复制的字符串),第二个参数是从字符串某一处开始复制,第三个参数是要复制字符串的长度(即个数) ...

  9. string类自定义字符串替换函数replace

    #include <iostream> #include <string> using namespace std; /* *  函数功能:将string字符串中的某些字符替换 ...

随机推荐

  1. 2016青岛网络赛 Barricade

    Barricade Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Proble ...

  2. Fix a Tree

    Fix a Tree A tree is an undirected connected graph without cycles. Let's consider a rooted undirecte ...

  3. android脚步---不同界面之间切换

    对于一个app,可能需要多个界面,使用Button或者其他控件在不同界面之间切换,那么如何做到呢 首先必须明确,一般一个activity.java文件一般只对应一个界面即一个layout.xml文件, ...

  4. Halcon相关

      1.Halcon的自我描述 Program Logic Ø Each program consists of a sequence of HALCON operators Ø The progra ...

  5. XPath语法

    XPath 是XML 的查询语言,和SQL 的角色很类似.以下面XML 为例,介绍XPath 的语法 <?xml version="1.0" encoding="I ...

  6. ural1003 Parity

    Parity Time limit: 2.0 secondMemory limit: 64 MB Now and then you play the following game with your ...

  7. 使用Bootstrap建立网站微金所——头部

    1.微金所链接:http://www.weijinsuo.com/ 2.头部分为:topbar和nav上下两个部分 (1)topbar详解 topbar使用bootstrap的栅格系统 (2)nav分 ...

  8. jQuery方式事件冒泡的2个方法

    方式1:通过  event.stopPropagation(); $("div").click(function (event) { slide.call(this); event ...

  9. HDU 5611 Baby Ming and phone number

    #include<cstdio> #include<cstring> #include<vector> #include<cmath> #include ...

  10. FZU 1058 粗心的物理学家

    这题有毒.要用long double定义,以及cout控制格式输出. #include<cstdio> #include<cstring> #include<cmath& ...