Standard C string String and MFC6.0 string CString tokenize and split functions.
1, standard string:
/ **********************************************************
The Tokenize Function for Std :: String
*********************************************************************************** / # include
Typedef Basic_String
TRIM indicates whether a null string is retained, and the default is reserved. Vector
Use a complete string Delimit to split the SRC string, no TRIM option, which is strictly divided. Vector
Vector
Vector
COUT << "------------- V1: << endl; for (int i = 0; i Try {string s = "###### 123 # 4 ### 56 ######## 789 ###"; string del = ""; // "###"; vector Return 0;} 2, cstring version: #include / * * This function splits S with the character of Delimits, and the CSTRINGLIST pointer PLIST is transmitted. * If Trim is true, the divided space is not retained (notice that it is not a blank character). For example: * tokenize ("A, BC; D,", ",;", & out_list, true) * will return 3 strings: a, bc, d. * If Trim is false, use nullsubst to replace the split empty string, such as: * tokenize ("A, BC;, D;", ",; & out_list, false," [null] ") * Returns 5 strings: A, BC, [NULL], D, [NULL]. * Trim defaults to false, nullsubst defaults to an empty string. * / void tokenize (cstring s, cstring delimits, cstringlist * plist, bool trim = false, cstring nullsubst = ") {assert (! s.isempty () &&! delimits.isempty ()); s = delimits [0 ]; For (long index = -1; (index = s.findoneof ((lpctstr) Delimits))! = - 1;) {if (index! = 0) PLIST-> AddTail (S.LEFT (Index)); Else if (! Trim) PLIST-> AddTail (Nullsubst); s = s.right (s.getLength () - index-1);}} / * * Similar Split () methods similar to the Java string. * Use a complete string Delimit to split the SRC string, no TRIM option, * is strictly divided. NUM is used to determine how many strings that are most split into, if it is 0 (default), follow the DELIMIT * segmentation, if it is 1, return the source string. * / void split (const cstring & src, cstring delimit, cstringlist * poutlist, int num = 0, cstring nullsubst = "") {assert (! src.isempty () &&! delimit.isempty ()); if (NUM == 1) {PoutList-> Addtail (src); return;} INT Delilen = delimit.getlength (); long index = -1, lastsearchPosition = 0, CNT = 0; while ((index = src.Find (delimit, lastSearchPosition)) = - 1!) {if (index == lastSearchPosition) pOutList-> AddTail (nullSubst); else pOutList-> AddTail (src.Mid (lastSearchPosition, index-lastSearchPosition )); LastSearchPosition = Index Delilen; IF (NUM) { CNT; IF (CNT 1 == Num) Break;}} cstring lastone = src.mid (LastSearchPosition); PoutList-> addtail (Lastone.Isempty ()? nullsubst: lastone);} / / Testint Main (void) {cstring s = ", ab; cde, f,,; gh,"; cstring sub = ",;; cstringlist list1, list2; tokenize (s, sub, & list1, true," no "); // <----- Printf (" ----- [tokenize_trim] ------- / n "); position pos1 = list1.getheadPosition (); while (POS1! = NULL) {Printf (List1.getNext (POS1)); Printf ("/ n");} tokenize (S, Sub, & List2, false, "[null]"); // <----- Printf "------ [tokenize_no_trim] ----- / n"); position pos2 = list2.getHeadPosition (); while (pOS2! = Null) {Printf (list2.getnext (pOS2)); printf (" / N ");} cstringlist list3; s =" ### 0123 ### 567 ###### 89 ### 1000 ### "; SUB =" ### "; split (s, sub, & list3, 3, "