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. Utils 工具 推送

    work_weipa_百度云推送 2014-09-05 17:55 7人阅读 评论(0) 收藏 举报 问题:怎么实现消息推送? 回答:下载sdk,根据文档操作即可 资料:http://develope ...

  2. Linux Mint 17.2个性化配置

    一.开启root 帐号登陆 设置一个口令,使用: sudo passwd root 当你使用完毕后屏蔽root帐号使用以下命令锁定root帐号 : sudo passwd -l root 如何在终端模 ...

  3. 关于Application.Lock…Application.Unlock有什么作用?

    因为Application变量里一般存储的是供所有连接到服务器的用户共享的信息(就像程序中所说的 "全局变量 "), 由于是全局变量,所以就容易出现两个或者多个用户同时对这一变量进 ...

  4. jquery的extend和fn.extend

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

  5. UIView你知道多少

    转载自:http://www.cnblogs.com/likwo/archive/2011/06/18/2084192.html   曾经有人这么说过,在iphone里你看到的,摸到的,都是UIVie ...

  6. (转)Permission denied: win7下面eclipse上传本地文件到DFS && 运行M/R程序时出现的同样的错误解决方法

    原文地址: http://mntms.iteye.com/blog/2095651 hadoopeclipse远程控制权限  情景一: 当在win7下面的eclipse装好插件,首次运行M/R程序的时 ...

  7. POJ 3254 Corn Fields(状态压缩)

    一道状态压缩的题,错了好多次....应该先把满足的情况预处理出来 #include<iostream> #include<cstdio> #include<cstring ...

  8. Chapter 1 First Sight——7

    Eventually we made it to Charlie's. 最终我们到了查理斯的家. He still lived in the small,two-bedroom house that ...

  9. @Resource @Autowired 区别

    spring2.5提供了基于注解(Annotation-based)的配置,我们可以通过注解的方式来完成注入依赖.在Java代码中可以使用 @Resource或者@Autowired注解方式来经行注入 ...

  10. 网页被卷去的高: document.body.scrollTop;

    网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...