日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C++實現字符串切割的兩種方法_C 語言

作者:SkyXU123123 ? 更新時間: 2022-08-22 編程語言

字符串切割的兩種方法

字符串切割的使用頻率還是挺高的,string本身沒有提供切割的方法,但可以使用stl提供的封裝進行實現或者通過c函數strtok()函數實現。

1、通過stl實現

涉及到string類的兩個函數find和substr:

1、find函數

  • 原型:size_t find ( const string& str, size_t pos = 0 ) const;
  • 功能:查找子字符串第一次出現的位置。
  • 參數說明:str為子字符串,pos為初始查找位置。
  • 返回值:找到的話返回第一次出現的位置,否則返回string::npos

2、substr函數

  • 原型:string substr ( size_t pos = 0, size_t len = npos ) const;
  • 功能:獲得子字符串。
  • 參數說明: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;
? ? }
? ? //方便截取最后一段數據
? ? 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()函數實現

  • 原型:char *strtok(char *str, const char *delim);
  • 功能:分解字符串為一組字符串。s為要分解的字符串,delim為分隔符字符串。
  • 描述:strtok()用來將字符串分割成一個個片段。參數s指向欲分割的字符串,參數delim則為分割字符串,當strtok()在參數s的字符串中發現到參數delim的分割字符時 則會將該字符改為\0 字符。在第一次調用時,strtok()必需給予參數s字符串,往后的調用則將參數s設置成NULL。每次調用成功則返回被分割出片段的指針。
  • 其它:strtok函數線程不安全,可以使用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;
};

字符串分割&類型轉換(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)); ? //創建一個istringstream對象,目的是將()中的字符串轉換為數字型
?? ??? ?// cout<<line.find("C")<<" ? ?"<<line.find(",")<<endl;
?? ??? ?double o_x, o_y, r;
?? ??? ?is1>>o_x; ? ? ? ? //將轉換后的數字輸入o_x
?? ??? ?cout<<o_x<<endl;
?? ??? ?line.erase(line.find("C"),line.find(",")+1);?? ?//將字符串中已經用過的部分擦除,為后面的字符串處理提供便利
?? ??? ?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默認0
  • erase(m,n) 表示從位置m開始擦除n個字符,返回字符串,m默認0
  • find(字符a)表示返回字符a所在的位置

原文鏈接:https://blog.csdn.net/xjw532881071/article/details/49154911

欄目分類
最近更新