C++實(shí)現(xiàn)字符串切割的兩種方法
字符串切割的兩種方法
字符串切割的使用頻率還是挺高的,string本身沒有提供切割的方法,但可以使用stl提供的封裝進(jìn)行實(shí)現(xiàn)或者通過c函數(shù)strtok()函數(shù)實(shí)現(xiàn)。
1、通過stl實(shí)現(xiàn)
涉及到string類的兩個(gè)函數(shù)find和substr:
1、find函數(shù)
- 原型:size_t find ( const string& str, size_t pos = 0 ) const;
- 功能:查找子字符串第一次出現(xiàn)的位置。
- 參數(shù)說明:str為子字符串,pos為初始查找位置。
- 返回值:找到的話返回第一次出現(xiàn)的位置,否則返回string::npos
2、substr函數(shù)
- 原型:string substr ( size_t pos = 0, size_t len = npos ) const;
- 功能:獲得子字符串。
- 參數(shù)說明:pos為起始位置(默認(rèn)為0),len為字符串長度(默認(rèn)為npos)
- 返回值:子字符串
代碼如下:
std::vector<std::string> splitWithStl(const std::string &str,const std::string &pattern) { ? ? std::vector<std::string> resVec; ?? ?if ("" == str) ? ? { ? ? ? ? return resVec; ? ? } ? ? //方便截取最后一段數(shù)據(jù) ? ? std::string strs = str + pattern; ? ?? ? ? size_t pos = strs.find(pattern); ? ? size_t size = strs.size(); ? ? while (pos != std::string::npos) ? ? { ? ? ? ? std::string x = strs.substr(0,pos); ? ? ? ? resVec.push_back(x); ? ? ? ? strs = strs.substr(pos+1,size); ? ? ? ? pos = strs.find(pattern); ? ? } ? ?? ? ? return resVec; }
2、通過使用strtok()函數(shù)實(shí)現(xiàn)
- 原型:char *strtok(char *str, const char *delim);
- 功能:分解字符串為一組字符串。s為要分解的字符串,delim為分隔符字符串。
- 描述:strtok()用來將字符串分割成一個(gè)個(gè)片段。參數(shù)s指向欲分割的字符串,參數(shù)delim則為分割字符串,當(dāng)strtok()在參數(shù)s的字符串中發(fā)現(xiàn)到參數(shù)delim的分割字符時(shí) 則會將該字符改為\0 字符。在第一次調(diào)用時(shí),strtok()必需給予參數(shù)s字符串,往后的調(diào)用則將參數(shù)s設(shè)置成NULL。每次調(diào)用成功則返回被分割出片段的指針。
- 其它:strtok函數(shù)線程不安全,可以使用strtok_r替代。
代碼如下:
std::vector<std::string> split(const std::string &str,const std::string &pattern) { ? ? //const char* convert to char* ? ? char * strc = new char[strlen(str.c_str())+1]; ? ? strcpy(strc, str.c_str()); ? ? std::vector<std::string> resultVec; ? ? char* tmpStr = strtok(strc, pattern.c_str()); ? ? while (tmpStr != NULL) ? ? { ? ? ? ? resultVec.push_back(std::string(tmpStr)); ? ? ? ? tmpStr = strtok(NULL, pattern.c_str()); ? ? } ? ?? ? ? delete[] strc; ? ?? ? ? return resultVec; };
字符串分割&類型轉(zhuǎn)換(string->double)
【自己備用】
代碼如下(示例):
#include<sstring>//頭文件 #include<iostream> using namespace std; int main() { ?? ?string line;? ?? ?ifstream is("2011_6.txt"); ?? ?while(is>>line) ?? ?{ ?? ??? ?cout<<line<<endl; ?? ??? ?istringstream ? is1(line.substr(line.find("C")+2,line.find(",")-2)); ? //創(chuàng)建一個(gè)istringstream對象,目的是將()中的字符串轉(zhuǎn)換為數(shù)字型 ?? ??? ?// cout<<line.find("C")<<" ? ?"<<line.find(",")<<endl; ?? ??? ?double o_x, o_y, r; ?? ??? ?is1>>o_x; ? ? ? ? //將轉(zhuǎn)換后的數(shù)字輸入o_x ?? ??? ?cout<<o_x<<endl; ?? ??? ?line.erase(line.find("C"),line.find(",")+1);?? ?//將字符串中已經(jīng)用過的部分擦除,為后面的字符串處理提供便利 ?? ??? ?cout<<line<<endl; ?? ??? ?//cout<<line.find(",")<<endl; ?? ??? ?istringstream is2(line.substr(0,line.find(","))); ?? ??? ?is2>>o_y; ?? ??? ?cout<<o_y<<endl; ?? ??? ?line.erase(0,line.find(",")+1); ?? ??? ?cout<<line<<endl; ?? ??? ?istringstream is3(line.substr(0,line.find(";"))); ?? ??? ?is3>>r; ?? ??? ?cout<<r<<endl; ?? ??? ?line.erase(0,line.find(";")+1); ?? ??? ?cout<<line<<endl; ?? ?} }
substr(m,n)
表示從位置m開始截取n個(gè)字符,返回字符串,m默認(rèn)0erase(m,n)
表示從位置m開始擦除n個(gè)字符,返回字符串,m默認(rèn)0find(字符a)
表示返回字符a所在的位置
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
數(shù)據(jù)結(jié)構(gòu)與算法 排序(冒泡,選擇,插入)
這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu)與算法 排序(冒泡,選擇,插入)的相關(guān)資料,這里對冒泡,選擇和插入都做有實(shí)例,需要的朋友可以參考下2017-07-07C/C++ Crypto密碼庫調(diào)用的實(shí)現(xiàn)方法
Crypto 庫是C/C++的加密算法庫,這個(gè)加密庫很流行,基本上涵蓋了市面上的各類加密解密算法,感興趣的可以參考一下2021-06-06C++ Boost PropertyTree解析INI文件詳解
Boost PropertyTree庫不僅可以解析JSON,XML格式,還可以直接解析INI格式文件。這篇文章就是為大家介紹一下如何通過Boost PropertyTree解析INI文件,需要的可以參考一下2022-01-01C++中vector<vector<int>?>的基本使用方法
vector<vector<int>?>其實(shí)就是容器嵌套容器,外層容器的元素類型是vector<int>,下面這篇文章主要給大家介紹了關(guān)于C++中vector<vector<int>?>的基本使用方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07windows下安裝QT及visual studio 2017搭建開發(fā)環(huán)境
這篇文章主要介紹了windows下安裝QT及visual studio 2017搭建開發(fā)環(huán)境,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03Qt圖形圖像開發(fā)之QT滾動區(qū)控件(滾動條)QScrollArea的詳細(xì)方法用法圖解與實(shí)例
這篇文章主要介紹了Qt圖形圖像開發(fā),QT滾動區(qū)控件(滾動條)QScrollArea的詳細(xì)方法用法圖解與實(shí)例,需要的朋友可以參考下2020-03-03