We often encounter the problem of string segmentation. Here we will summarize it for my convenience.
1. Use the strtok function to separate strings
Prototype: char * strtok (char * STR, const char * delim );
Function: Splits a string into a group of strings.
Parameter description: STR is the string to be decomposed, and delim is the separator string.
Return Value: Split strings starting with 'str. If no split string exists, null is returned.
Others: the strtok function thread is not secure and can be replaced by strtok_r.
Example:
1 // use strtok to implement split
2 # include <string. h>
3 # include <stdio. h>
4
5 Int main ()
6 {
7 char s [] = "golden global view, disk * desk ";
8 const char * D = ",*";
9 char * P;
10 p = strtok (S, d );
11 while (P)
12 {
13 printf ("% s \ n", P );
14 p = strtok (null, d );
15}
16
17 return 0;
18}
Running effect:
2. Use STL to separate strings
The following two functions are involved: Find and substr:
1. Find function
Prototype: size_t find (const string & STR, size_t Pos = 0) const;
Function: locate the first occurrence of a substring.
Parameter description: STR is a substring, and POS is the initial search position.
Returned value: if it is found, the first position is returned. Otherwise, string: NPOs is returned.
2. substr Function
Prototype: String substr (size_t Pos = 0, size_t n = NPOs) const;
Function: obtain a substring.
Parameter description: POS is the starting position (0 by default), and N is the ending position (NPOs by default)
Return Value: substring
The implementation is as follows:
1 // string segmentation Function
2 STD: vector <STD: String> split (STD: String STR, STD: String Pattern)
3 {
4 STD: String: size_type Pos;
5 STD: vector <STD: String> result;
6 STR + = pattern; // extend the string for easy operation
7 int size = Str. Size ();
8
9 For (INT I = 0; I <size; I ++)
10 {
11 Pos = Str. Find (pattern, I );
12 if (Pos <size)
13 {
14 STD: String S = Str. substr (I, pos-I );
15 result. push_back (s );
16 I = POS + pattern. Size ()-1;
17}
18}
19 return result;
20}
Complete code:
View code
Running effect:
3. Use boost to separate strings
Use the regular expression of the boost library to split strings
The implementation is as follows:
1 std::vector<std::string> split(std::string str,std::string s)
2 {
3 boost::regex reg(s.c_str());
4 std::vector<std::string> vec;
5 boost::sregex_token_iterator it(str.begin(),str.end(),reg,-1);
6 boost::sregex_token_iterator end;
7 while(it!=end)
8 {
9 vec.push_back(*it++);
10 }
11 return vec;
12 }
Complete code:
1 // This program implements the use of regular expressions to achieve string segmentation
2 // run the vc6.0 + boost Library
3 /*
4 file: split2.cpp
5 Author: Mike
6 E-mail: [email protected]
7 */
8 # include <iostream>
9 # include <cassert>
10 # include <vector>
11 # include <string>
12 # include "Boost/RegEx. HPP"
13
14 STD: vector <STD: String> split (STD: String STR, STD: String S)
15 {
16 boost: RegEx Reg (S. c_str ());
17 STD: vector <STD: String> VEC;
18 boost: sregex_token_iterator it (Str. Begin (), str. End (), Reg,-1 );
19 boost: sregex_token_iterator end;
20 while (it! = End)
21 {
22 Vec. push_back (* It ++ );
23}
24 return VEC;
25}
26 int main ()
27 {
28 STD: String STR, S;
29 STR = "SSS/DDD/ggg/HH ";
30 s = "/";
31 STD: vector <STD: String> VEC = Split (STR, S );
32 For (INT I = 0, size = Vec. Size (); I <size; I ++)
33 {
34 STD: cout <VEC [I] <STD: Endl;
35}
36 STD: cin. Get ();
37 STD: cin. Get ();
38 return 0;
39}
Running effect:
Supplement:
Recently, we found that boost has built-in split functions. If boost is used, it is better to use split directly. I will not talk about it here. The Code is as follows:
#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;}
Okay, that's all. I hope it will help you.
C. Partial string operations