C++實現(xiàn)字符串切割的兩種方法
更新時間:2022年06月30日 14:18:12 作者:SkyXU123123
這篇文章主要介紹了C++實現(xiàn)字符串切割的兩種方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
字符串切割的兩種方法
字符串切割的使用頻率還是挺高的,string本身沒有提供切割的方法,但可以使用stl提供的封裝進行實現(xiàn)或者通過c函數(shù)strtok()函數(shù)實現(xiàn)。
1、通過stl實現(xiàn)
涉及到string類的兩個函數(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為起始位置(默認為0),len為字符串長度(默認為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ù)實現(xiàn)
- 原型:char *strtok(char *str, const char *delim);
- 功能:分解字符串為一組字符串。s為要分解的字符串,delim為分隔符字符串。
- 描述:strtok()用來將字符串分割成一個個片段。參數(shù)s指向欲分割的字符串,參數(shù)delim則為分割字符串,當strtok()在參數(shù)s的字符串中發(fā)現(xiàn)到參數(shù)delim的分割字符時 則會將該字符改為\0 字符。在第一次調(diào)用時,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)建一個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個字符,返回字符串,m默認0erase(m,n)表示從位置m開始擦除n個字符,返回字符串,m默認0find(字符a)表示返回字符a所在的位置
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
wince程序防止創(chuàng)建多個實例實現(xiàn)互斥作用
什么時候用的互斥?當你的程序只允許同時打開一個的時候,就可以通過互斥來實現(xiàn),下面說的互斥,主要是針對防止程序創(chuàng)建多個實例這種情況來實現(xiàn)的2014-02-02
C/C++?string.h庫中memcpy()和memmove()的使用
memcpy與memmove的目的都是將N個字節(jié)的源內(nèi)存地址的內(nèi)容拷貝到目標內(nèi)存地址中,本文主要介紹了C/C++?string.h庫中memcpy()和memmove()的使用,感興趣的可以了解一下2023-12-12
VScode搭建C/C++開發(fā)環(huán)境的詳細過程
最近迷上了vscode,小巧美觀,最主要的是全平臺,但是vscode并不是ide,必須得自己配置環(huán)境,下面這篇文章主要給大家介紹了關(guān)于VScode搭建C/C++開發(fā)環(huán)境的詳細過程,需要的朋友可以參考下2023-06-06
C語言每日練習(xí)之統(tǒng)計文本單詞數(shù)及高頻詞
本文文大家準備了個C語言練習(xí)題:統(tǒng)計單詞數(shù)并找出頻率最高的單詞,文中的示例代碼講解詳細,對我們學(xué)習(xí)C語言有一定幫助,感興趣的可以了解一下2022-05-05

