1、用strtok函數進行字符串分割
原型: char *strtok(char *str, const char *delim);
功能:分解字符串為一組字符串。
參數說明:str為要分解的字符串,delim為分隔符字符串。
返回值:從str開頭開始的一個個被分割的串。當沒有被分割的串時則返回NULL。
其它:strtok函數線程不安全,可以使用strtok_r替代。
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
//借助strtok實現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;
}
|
2、substr函數
原型:string substr ( size_t pos = 0, size_t n = npos ) const;
功能:獲得子字符串。
參數說明:pos為起始位置(默認為0),n為結束位置(默認為npos)
返回值:子字符串
#include <iostream> #include <string> #include <vector> //字符串分割函數 std::vector<std::string> split(std::string str, std::string pattern) { std::string::size_type pos; std::vector<std::string> result; str += pattern;//擴展字符串以方便操作 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;
}
3、find_first_not_of
#include<string> #include<vector> #include<iostream> using namespace std; void Tokenize(const string& str, vector<string>& tokens, const string& delimiters) { // Skip delimiters at beginning. string::size_type lastPos = str.find_first_not_of(delimiters, 0); // Find first "non-delimiter". string::size_type pos = str.find_first_of(delimiters, lastPos); while (string::npos != pos || string::npos != lastPos) { // Found a token, add it to the vector. tokens.push_back(str.substr(lastPos, pos - lastPos)); // Skip delimiters. Note the "not_of" lastPos = str.find_first_not_of(delimiters, pos); // Find next "non-delimiter" pos = str.find_first_of(delimiters, lastPos); } } int main(int argc, char *argv[]) { string str("====aaa==bbb=ccc=ddd===="); vector<string>tokens; Tokenize(str, tokens, "="); for( int i = 0; i < tokens.size() ; i++ ) { cout << tokens[i] << endl; } return 0; }
4、用Boost進行字符串的分割
用boost庫的正則表達式實現字符串分割
#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;
}
5、最近發現boost里面有自帶的split的函數,如果用boost的話,還是直接用split的好,代碼如下:
#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; }