網站首頁 編程語言 正文
字符串切割的兩種方法
字符串切割的使用頻率還是挺高的,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
相關推薦
- 2022-09-15 windows中cmd下添加、刪除和修改靜態路由實現_DOS/BAT
- 2022-08-21 Golang切片刪除指定元素的三種方法對比_Golang
- 2022-07-17 代碼解析python標準庫logging模塊_python
- 2022-12-12 C++?Boost?Chrono實現計時碼表流程詳解_C 語言
- 2022-11-29 ASP.NET?Identity的基本用法_實用技巧
- 2022-11-21 C#基礎知識之字符串和正則表達式_C#教程
- 2022-03-15 Centos7下NFS服務搭建介紹_Linux
- 2022-02-26 SpringSecurity 自定義JwtAuthorFilter基于JWT的Token驗證
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支