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.
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;
17}
18}
19 return result;
20}
Complete code:
View Code
1 /*
2 File: split1.cpp
3 Author: Mike
4 E-Mail: Mike_Zhang@live.com
5 */
6 # include <iostream>
7 # include <string>
8 # include <vector>
9
10 // string segmentation Function
11 std: vector <std: string> split (std: string str, std: string pattern)
12 {
13 std: string: size_type pos;
14 std: vector <std: string> result;
15 str + = pattern; // extended string for easy operation
16 int size = str. size ();
17
18 for (int I = 0; I <size; I ++)
19 {
20 pos = str. find (pattern, I );
21 if (pos <size)
22 {
23 std: string s = str. substr (I, pos-I );
24 result. push_back (s );
25 I = pos;
26}
27}
28 return result;
29}
30
31 int main ()
32 {
33 std: string str;
34 std: cout <"Please input str:" <std: endl;
35 // std: cin> str;
36 getline (std: cin, str );
37 std: string pattern;
38 std: cout <"Please input pattern:" <std: endl;
39 // std: cin> pattern;
40 getline (std: cin, pattern); // used to obtain a string containing Spaces
41 std: vector <std: string> result = split (str, pattern );
42 std: cout <"The result:" <std: endl;
43 for (int I = 0; I <result. size (); I ++)
44 {
45 std: cout <result [I] <std: endl;
46}
47
48 std: cin. get ();
49 std: cin. get ();
50 return 0;
51}
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:
View 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: Mike_Zhang@live.com
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:
Okay, that's all. I hope it will help you.
From MikeZhang's blog