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. 如何添加在eclipse 中添加 window Builder

    将features文件夹和plugins文件夹添加到eclipse的dropins文件夹下 然后再用专业的软件来破解 提供软件: WindowBuilderKeygen.exe

  2. Barnicle

    Barnicle Barney is standing in a bar and starring at a pretty girl. He wants to shoot her with his h ...

  3. RocketMQ源码 — 三、 Producer消息发送过程

    Producer 消息发送 producer start producer启动过程如下图 public void start(final boolean startFactory) throws MQ ...

  4. 一些简单的例子让你在Java中能更好的学习并理解循环结构(1)!

    一.java中流程控制方式采用三种基本流程结构:顺序结构,选择(分支)结构,循环结构. 1.[if-else 结构] if(1>2){ system.out.println("if条件 ...

  5. VS找不到MFC90d.dll错误

    VS 2005/VS 2008在生成可执行文件时使用了一种新的技术,该技术生成的可执行文件会伴随生成一个清单文件(manifest file)(.manifest后缀文件)(其本质上是XML文档,你可 ...

  6. Android网络开发之OkHttp--基本用法实例化各个对象

    1.实例化OkHttpClient对象,OkHttpClient包含了以下属性,以及set()和get()方法.但并没有包含具体的执行方法,详情见源码. //实例化OkHttpClent对象 priv ...

  7. 2快速掌握OMD

    我们已经知道使用ArcGIS Engine开发,也就意味着我们要和接口打交道,ArcGIS Engine中提供的接口和类加起来估计上万,但是用过ArcGIS Engine的人,知道这个数字不为过.Ar ...

  8. C#使用FFmpeg 将视频格式转换成MP4示例

    一.常用视频格式分辨率 640x480p 720p格式,分辨率为1280×720p / 60Hz,行频为45kHz 1080p格式,分辨率为1920×1080逐行扫描,专业格式 二.FFmpeg部分参 ...

  9. psql命令

    原文:http://blog.csdn.net/smstong/article/details/17138355 psql# shell 环境下,查看当前所在的数据库的命令是: select  cur ...

  10. gtk

    GTK官网:www.gtk.org sudo apt-get install build-essential 安装GTK开发套件: sudo apt-get install libgtk2.0-dev ...