1. String segmentation with Strtok function
Prototype: Char *strtok (char *str, const char *delim);
Function: Decomposes a string into a set of strings.
Parameter description: STR is the string to be decomposed, Delim is the delimiter string.
Return value: A segmented string starting at the beginning of the Str. Null is returned when there is no split string.
Other: strtok function thread is unsafe, you can use Strtok_r instead.
Example:
123456789101112131415161718 |
//借助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 function
Prototype: String substr (size_t pos = 0, size_t n = npos) const;
function: Gets substring.
Parameter description: POS is the starting position (default is 0), n is the end position (default is NPOs)
return value: substring
#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 namespacestd;voidTokenize (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); }}intMainintargcChar*argv[]) { stringStr"====aaa==bbb=ccc=ddd===="); Vector<string>tokens; Tokenize (str, tokens,"="); for(inti =0; I < tokens.size (); i++) {cout<< Tokens[i] <<Endl; } return 0;}
4. Using boost for string segmentation
Using the regular expression of the boost library to implement the string segmentation
#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, recently found that the boost has its own split function, if using boost, or directly with Split's good, the code is as follows:
#include <iostream>#include<string>#include<vector>#include<boost/algorithm/string/classification.hpp>#include<boost/algorithm/string/split.hpp>using namespacestd;intMain () {strings ="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;}
Translated from http://www.jb51.net/article/55954.htm
http://blog.csdn.net/wyx819/article/details/12846137
C + + Common string segmentation method (GO)