字串替換函數

來源:互聯網
上載者:User

標籤:替換字串

// 參數說明:  

// in, 源字串  

// out, 存放最後結果的字串  

// outlen,out最大的大小  

// src,要替換的字串  

// dst,替換成什麼字串  

/********************************************************************  

*  Function:   str_replace()  

*  Description: 在一個字串中尋找一個子串,並且把所有符合的子串用  

                另一個替換字串替換。  

*  Calls:        memcpy();  

*  Called By:    無  

*  Input:       p_source:要尋找的母字串; p_seach要尋找的子字串;  

                 p_repstr:替換的字串;  

*  Output:      p_result:存放結果;  

*  Return :      返回替換成功的子串數量;  

*  Author:       ChenZhiFa  

*  Others:       p_result要足夠大的空間存放結果,所以輸入參數都要以\0結束;  

*  date of completion:   

*********************************************************************/  

int str_replace(char *p_result,char* p_source,char* p_seach,char *p_repstr)  

{  

  

    int c = 0;  

    int repstr_leng = 0;  

    int searchstr_leng = 0;  

  

    char *p1;  

    char *presult = p_result;  

    char *psource = p_source;  

    char *prep = p_repstr;  

    char *pseach = p_seach;  

    int nLen = 0;   

  

    repstr_leng = strlen(prep);  

    searchstr_leng = strlen(pseach);  

  

    do{   

        p1 = strstr(psource,p_seach);  

  

        if (p1 == 0)  

        {  

            strcpy(presult,psource);  

            return c;  

        }  

  

        c++;  //匹配子串計數加1;  

 //       printf("p_result:%s\r\n",p_result);  

 //       printf("p_source:%s\r\n",p_source);  

  

        // 拷貝上一個替換點和下一個替換點中間的字串  

        nLen = p1 - psource;  

        memcpy(presult, psource, nLen);  

  

        // 拷貝需要替換的字串  

        memcpy(presult + nLen,p_repstr,repstr_leng);  

  

        psource = p1 + searchstr_leng;  

        presult = presult + nLen + repstr_leng;  

  

 

    }while(p1);  

  

    return c;  

  

} 


字串替換函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.