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. jquery的extend和fn.extend

    jQuery为开发插件提拱了两个方法,分别是: jQuery.fn.extend(object); jQuery.extend(object); jQuery.extend(object); 为扩展j ...

  2. POJ 2296 Map Labeler

    二分答案 + 2-SAT验证,判断正方形是否相交写起来有点烦,思路还是挺简单的. #include<cstdio> #include<cstring> #include< ...

  3. 随机获取部分List<Object>集合

    随机返回list对象 /** * 返回随机List * @param list 备选 * @param selected 备选数量 * @return */ public List getRandom ...

  4. json python api

    摘要:对于python来说,json并不是一种数据类型,可以把它视为函数.json.dumps把字典或列表变成json风格的str类型:json.loads把json风格的str类型变成原来的类型(列 ...

  5. eclipse's code assist

    突然发现有个类没有code assist功能了,而别的类都还有,新建的类也有,可是当把代码拷贝到新建的类还是不行:尝试了各种办法,包括删除workspace/.metadata/.plugin/org ...

  6. swfobject.embedSWF属性与用法

    JS+flash的焦点幻灯片既能大方得体的展示焦点信息,也能美轮美奂的展示图片,越来越多的网站使用这种焦点幻灯的表现方法.很多童鞋在下载这方面的素材代码的时候,往往会因为展示出来的是flash,觉得难 ...

  7. Java谜题——类谜题(二)

    1.域的隐藏 代码如下: class Base { public String className = "Base"; } class Derived extends Base { ...

  8. Java谜题——库谜题

    1.Java中的不可变对象和可变对象 (1)不可变类:当你获得这个类的实例的引用之后,你不可以改变这个实例的内容.比如:String,BigInteger,BigDecimal,还有基本数据类型的封装 ...

  9. tensorflow的Virtualenv安装方式安装

    本文介绍了如何在ubuntu上以virtualenv方式安装tensorflow. 安装pip和virtualenv: # Ubuntu/Linux 64-bit sudo apt-get insta ...

  10. Codeforces AIM Tech Round3

    打得最烂一场Codeforces,多次都错题,无限WA... A题: 题意:给定n个橘子的大小,大小超过b的丢掉,不足d的补充进来,同时超过d的部分去掉,问要去掉几次 分析:直接模拟即可 #inclu ...