First, using Strtok function for string segmentation
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.
//using Strtok to achieve split#include <string.h>#include<stdio.h>intMain () {CharS[] ="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;}
Second, using STL to divide the string
Two functions involving the string class find and substr:
1. Find function
Prototype: size_t find (const string& str, size_t pos = 0) const;
Function: Finds the position where the substring first appears.
Parameter description: STR is a substring and POS is the initial find location.
Return value: If found, returns the first occurrence of the position, otherwise returns String::npos
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>//string splitting functionSTD::VECTOR<STD::string> Split (std::stringSTR,STD::stringpattern) {std::string:: Size_type pos; Std::vector<STD::string>result; STR+=pattern;//extending strings for ease of operation intSize=str.size (); for(intI=0; i<size; i++) {pos=Str.find (pattern,i); if(pos<size) {std::stringS=str.substr (i,pos-i); Result.push_back (s); I=pos+pattern.size ()-1; } } returnresult;} intMain () {std::stringstr; Std::cout<<"Please input str:"<<Std::endl; //std::cin>>str;getline (STD::CIN,STR); STD::stringpattern; Std::cout<<"Please input pattern:"<<Std::endl; //std::cin>>pattern;Getline (Std::cin,pattern);//used to get a string with spacesSTD::VECTOR<STD::string> result=split (Str,pattern); Std::cout<<"The result:"<<Std::endl; for(intI=0; I<result.size (); i++) {Std::cout<<result[i]<<Std::endl; } std::cin.Get(); Std::cin.Get(); return 0;}
Third, 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::stringSTR,STD::strings) {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++); } returnVec;}intMain () {std::stringstr,s; STR="sss/ddd/ggg/hh"; S="/"; Std::vector<STD::string> vec=split (str,s); for(intI=0, Size=vec.size (); i<size;i++) {Std::cout<<vec[i]<<Std::endl; } std::cin.Get(); Std::cin.Get(); return 0;}
Boost has its own split function, and if you use boost, it's better to use split directly.
#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;}
Using the Boost library
1) Use Boost::split. Split by character, note that is_any_of means any one in the collection, not equals, how do you split the original string based on the string? Please see 2 and 3.
#include <iostream>#include<vector>#include<string>#include<boost/algorithm/string.hpp>using namespacestd;intMain () {stringStr"DFA&FDA|DFDF"); Vector<string>tokens; Boost::split (tokens, str, boost::is_any_of ("&|")); for(size_t i =0; I < tokens.size (); ++i) {cout<< Tokens[i] <<Endl; } return 0;}
2) using Boost::tokenize, note that the boost::char_separator<char> delimiter is the same as the concept of a collection! But the partitioning method and Boost::split are not the same, look at the boost document.
#include <iostream>#include<vector>#include<string>#include<boost/tokenizer.hpp>using namespacestd;intMain () {stringStr"dfa| | fda| | DFDF"); Vector<string>tokens; Boost::char_separator<Char> Sep ("|"); Boost::tokenizer<boost::char_separator<Char> >Tok (str, SEP); Tokens.clear (); Std::copy (Tok.begin (), Tok.end (), Std::back_inserter (tokens)); for(size_t i =0; I < tokens.size (); ++i) {cout<< Tokens[i] <<Endl; } return 0;}
3) Use Boost::regex
#include <iostream>#include<vector>#include<string>#include<boost/regex.hpp>using namespacestd;intMain () {stringStr"dfa| | fda| | DFDF"); Vector<string>tokens; Boost::regex Reg ("\\|\\|"); Boost::sregex_token_iterator It (Str.begin (), Str.end (), Reg,-1); Boost::sregex_token_iterator end; while(it!=end) {Tokens.push_back (*it++); } for(size_t i =0; I < tokens.size (); ++i) {cout<< Tokens[i] <<Endl; } return 0;}
C + + Common string Segmentation method