基于C++字符串替換函數(shù)的使用詳解
更新時(shí)間:2013年05月17日 17:49:50 作者:
本篇文章是對(duì)C++字符串替換函數(shù)的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
在C++中,字符串替換有很多方法,這里主要說一下STL里的WString中的替換,雖然WString自帶了一個(gè)Replace函數(shù),但是只能替換一次,太不好了,因此單獨(dú)寫了個(gè)替換函數(shù)
[函數(shù)]
/**
* @brief 實(shí)現(xiàn)字符串替換
* @param orignStr 源串
* @param oldStr 查找的串
* @param newStr 替換的新串
* @return 返回修改后的串
*/
static wstring Replace(const wstring& orignStr, const wstring& oldStr, const wstring& newStr);
[實(shí)現(xiàn)]
std::wstring Replace( const wstring& orignStr, const wstring& oldStr, const wstring& newStr )
{
size_t pos = 0;
wstring tempStr = orignStr;
wstring::size_type newStrLen = newStr.length();
wstring::size_type oldStrLen = oldStr.length();
while(true)
{
pos = tempStr.find(oldStr, pos);
if (pos == wstring::npos) break;
tempStr.replace(pos, oldStrLen, newStr);
pos += newStrLen;
}
return tempStr;
}
[函數(shù)]
復(fù)制代碼 代碼如下:
/**
* @brief 實(shí)現(xiàn)字符串替換
* @param orignStr 源串
* @param oldStr 查找的串
* @param newStr 替換的新串
* @return 返回修改后的串
*/
static wstring Replace(const wstring& orignStr, const wstring& oldStr, const wstring& newStr);
[實(shí)現(xiàn)]
復(fù)制代碼 代碼如下:
std::wstring Replace( const wstring& orignStr, const wstring& oldStr, const wstring& newStr )
{
size_t pos = 0;
wstring tempStr = orignStr;
wstring::size_type newStrLen = newStr.length();
wstring::size_type oldStrLen = oldStr.length();
while(true)
{
pos = tempStr.find(oldStr, pos);
if (pos == wstring::npos) break;
tempStr.replace(pos, oldStrLen, newStr);
pos += newStrLen;
}
return tempStr;
}
相關(guān)文章
C++ 中boost::share_ptr智能指針的使用方法
這篇文章主要介紹了C++ 中boost::share_ptr智能指針的使用方法的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-10-10Matlab控制電腦攝像實(shí)現(xiàn)實(shí)時(shí)人臉檢測(cè)和識(shí)別詳解
人臉識(shí)別過程主要由四個(gè)階段組成:人臉檢測(cè)、圖像預(yù)處理、面部特征提取和特征識(shí)別。這篇文章主要介紹了如何使用MATLAB控制筆記本電腦的攝像頭,并進(jìn)行實(shí)時(shí)人臉檢測(cè)和識(shí)別,需要的可以參考一下2022-10-10c++將引用或者是指針作為函數(shù)參數(shù)實(shí)現(xiàn)實(shí)參的運(yùn)算
這篇文章主要介紹了c++將引用或者是指針作為函數(shù)參數(shù)實(shí)現(xiàn)實(shí)參的運(yùn)算,需要的朋友可以參考下2014-05-05C++利用opencv實(shí)現(xiàn)單目測(cè)距的實(shí)現(xiàn)示例
本文主要介紹了C++利用opencv實(shí)現(xiàn)單目測(cè)距的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11