C++常用字符串分割方法實(shí)例匯總
本文實(shí)例匯總了C++常用字符串分割方法,分享給大家供大家參考。具體分析如下:
我們?cè)诰幊痰臅r(shí)候經(jīng)常會(huì)碰到字符串分割的問(wèn)題,這里總結(jié)下,也方便我們以后查詢使用。
一、用strtok函數(shù)進(jìn)行字符串分割
原型: char *strtok(char *str, const char *delim);
功能:分解字符串為一組字符串。
參數(shù)說(shuō)明:str為要分解的字符串,delim為分隔符字符串。
返回值:從str開(kāi)頭開(kāi)始的一個(gè)個(gè)被分割的串。當(dāng)沒(méi)有被分割的串時(shí)則返回NULL。
其它:strtok函數(shù)線程不安全,可以使用strtok_r替代。
示例:
//借助strtok實(shí)現(xiàn)split
#include <string.h>
#include <stdio.h>
int main()
{
char s[] = "Golden Global View,disk * desk";
const char *d = " ,*";
char *p;
p = strtok(s,d);
while(p)
{
printf("%s\n",p);
p=strtok(NULL,d);
}
return 0;
}
運(yùn)行效果如下圖所示:
二、用STL進(jìn)行字符串的分割
涉及到string類的兩個(gè)函數(shù)find和substr:
1、find函數(shù)
原型:size_t find ( const string& str, size_t pos = 0 ) const;
功能:查找子字符串第一次出現(xiàn)的位置。
參數(shù)說(shuō)明:str為子字符串,pos為初始查找位置。
返回值:找到的話返回第一次出現(xiàn)的位置,否則返回string::npos
2、substr函數(shù)
原型:string substr ( size_t pos = 0, size_t n = npos ) const;
功能:獲得子字符串。
參數(shù)說(shuō)明:pos為起始位置(默認(rèn)為0),n為結(jié)束位置(默認(rèn)為npos)
返回值:子字符串
實(shí)現(xiàn)如下:
//字符串分割函數(shù)
std::vector<std::string> split(std::string str,std::string pattern)
{
std::string::size_type pos;
std::vector<std::string> result;
str+=pattern;//擴(kuò)展字符串以方便操作
int size=str.size();
for(int i=0; i<size; i++)
{
pos=str.find(pattern,i);
if(pos<size)
{
std::string s=str.substr(i,pos-i);
result.push_back(s);
i=pos+pattern.size()-1;
}
}
return result;
}
完整代碼:
/*
File : split1.cpp
Author : Mike
E-Mail : Mike_Zhang@live.com
*/
#include <iostream>
#include <string>
#include <vector>
//字符串分割函數(shù)
std::vector<std::string> split(std::string str,std::string pattern)
{
std::string::size_type pos;
std::vector<std::string> result;
str+=pattern;//擴(kuò)展字符串以方便操作
int size=str.size();
for(int i=0; i<size; i++)
{
pos=str.find(pattern,i);
if(pos<size)
{
std::string s=str.substr(i,pos-i);
result.push_back(s);
i=pos+pattern.size()-1;
}
}
return result;
}
int main()
{
std::string str;
std::cout<<"Please input str:"<<std::endl;
//std::cin>>str;
getline(std::cin,str);
std::string pattern;
std::cout<<"Please input pattern:"<<std::endl;
//std::cin>>pattern;
getline(std::cin,pattern);//用于獲取含空格的字符串
std::vector<std::string> result=split(str,pattern);
std::cout<<"The result:"<<std::endl;
for(int i=0; i<result.size(); i++)
{
std::cout<<result[i]<<std::endl;
}
std::cin.get();
std::cin.get();
return 0;
}
運(yùn)行效果如下圖所示:
三、用Boost進(jìn)行字符串的分割
用boost庫(kù)的正則表達(dá)式實(shí)現(xiàn)字符串分割
實(shí)現(xiàn)如下:
std::vector<std::string> split(std::string str,std::string s)
{
boost::regex reg(s.c_str());
std::vector<std::string> vec;
boost::sregex_token_iterator it(str.begin(),str.end(),reg,-1);
boost::sregex_token_iterator end;
while(it!=end)
{
vec.push_back(*it++);
}
return vec;
}
完整代碼:
//本程序?qū)崿F(xiàn)的是利用正則表達(dá)式對(duì)字符串實(shí)現(xiàn)分割
//運(yùn)行環(huán)境 VC6.0 + boost 庫(kù)
/*
File : split2.cpp
Author : Mike
E-Mail : Mike_Zhang@live.com
*/
#include <iostream>
#include <cassert>
#include <vector>
#include <string>
#include "boost/regex.hpp"
std::vector<std::string> split(std::string str,std::string s)
{
boost::regex reg(s.c_str());
std::vector<std::string> vec;
boost::sregex_token_iterator it(str.begin(),str.end(),reg,-1);
boost::sregex_token_iterator end;
while(it!=end)
{
vec.push_back(*it++);
}
return vec;
}
int main()
{
std::string str,s;
str="sss/ddd/ggg/hh";
s="/";
std::vector<std::string> vec=split(str,s);
for(int i=0,size=vec.size();i<size;i++)
{
std::cout<<vec[i]<<std::endl;
}
std::cin.get();
std::cin.get();
return 0;
}
運(yùn)行效果如下圖所示:
補(bǔ)充:
最近發(fā)現(xiàn)boost里面有自帶的split的函數(shù),如果用boost的話,還是直接用split的好,這里就不多說(shuō)了,代碼如下:
#include <iostream>
#include <string>
#include <vector>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
using namespace std;
int main()
{
string s = "sss/ddd,ggg";
vector<string> vStr;
boost::split( vStr, s, boost::is_any_of( ",/" ), boost::token_compress_on );
for( vector<string>::iterator it = vStr.begin(); it != vStr.end(); ++ it )
cout << *it << endl;
return 0;
}
希望本文所述對(duì)大家的C++程序設(shè)計(jì)有所幫助。
上一篇:沒(méi)有了
欄 目:C語(yǔ)言
本文標(biāo)題:C++常用字符串分割方法實(shí)例匯總
本文地址:http://m.jygsgssxh.com/a1/Cyuyan/3319.html
您可能感興趣的文章
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 01-10深入理解C++中常見(jiàn)的關(guān)鍵字含義
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10深入解析最長(zhǎng)公共子串
- 01-10c++中inline的用法分析
- 01-10用C++實(shí)現(xiàn)DBSCAN聚類算法
- 01-10全排列算法的非遞歸實(shí)現(xiàn)與遞歸實(shí)現(xiàn)的方法(C++)
- 01-10C++大數(shù)模板(推薦)
- 01-10淺談C/C++中的static與extern關(guān)鍵字的使用詳解
- 01-10深入C/C++浮點(diǎn)數(shù)在內(nèi)存中的存儲(chǔ)方式詳解


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)
- 04-02c語(yǔ)言用函數(shù)寫(xiě)分段 用c語(yǔ)言表示分段
- 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)
- 04-02c語(yǔ)言編寫(xiě)函數(shù)冒泡排序 c語(yǔ)言冒泡排
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段
- 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎
- 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求
隨機(jī)閱讀
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10C#中split用法實(shí)例總結(jié)


