欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++實(shí)現(xiàn)字符串切割的兩種方法

 更新時(shí)間:2022年06月30日 14:18:12   作者:SkyXU123123  
這篇文章主要介紹了C++實(shí)現(xiàn)字符串切割的兩種方法,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

字符串切割的兩種方法

字符串切割的使用頻率還是挺高的,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)0
  • erase(m,n) 表示從位置m開始擦除n個(gè)字符,返回字符串,m默認(rèn)0
  • find(字符a)表示返回字符a所在的位置

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論