Standard C ++ and MFC6.0 string tokenize and split functions

xiaoxiao2021-03-06  14

Standard C string String and MFC6.0 string CString tokenize and split functions.

1, standard string:

/ **********************************************************

The Tokenize Function for Std :: String

*********************************************************************************** / # include #include #include using namespace std;

Typedef Basic_String :: size_type s_t; static const S_t npos = -1;

TRIM indicates whether a null string is retained, and the default is reserved. Vector Tokenize (const string & src, string tok, bool trim = false, string null_subst = ") {if (src.empty () || tok.empty ()) throw" tokenize: Empty string / 0 "; Vector v; s_t pre_index = 0, index = 0, len = 0; while ((index = src.find_first_of (tok, pre_index))! = npos) {if ((len = index-pre_index)! = 0 ) V.push_back (pe_index, len)); Else if (Trim == false) v.push_back (null_subst); pre_index = index 1;} string endstr = src.substr (pre_index); if (Trim == false) v.push_back (endstr.empty ()? null_subst: endstr); Else if (! endstr.empty ()) v.push_back (endstr); return v;}

Use a complete string Delimit to split the SRC string, no TRIM option, which is strictly divided. Vector split (const string & src, string delimit, string null_subst = ") {if (src.empty () || DELIMIT.EMPTY ()) throw" split: Empty string / 0 ";

Vector v; s_t deli_len = delimit.size (); long index = npos, last_search_position = 0; while ((index = src.find (delimit, last_search_position))! = npos) {if (index == last_search_position) v.push_back (null_subst); else v.push_back (src.substr (last_search_position, index-last_search_position)); last_search_position = index deli_len;} string last_one = src.substr (last_search_position); v.push_back (last_one.empty () NULL_SUBST: LAST_ONE); RETURN V;} // Testint Main (Void) {String SRC = ", AB, CDE;, FG,"; String tok = ",;"

Vector v1 = tokenize (src, tok, true); Vector V2 = Tokenize (SRC, TOK, FALSE, "");

COUT << "------------- V1: << endl; for (int i = 0; i

Try {string s = "###### 123 # 4 ### 56 ######## 789 ###"; string del = ""; // "###"; vector v3 = split (s, del, ""); cout << "------------- v3:" << endl; for (int K = 0; k

Return 0;}

2, cstring version:

#include #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, ""); // <----- Printf ("------ [split] ----- / n"); Position POS3 = list3.getHeadPosition (); While (POS3! = null) {printf (list3.getnext (pOS3)); Printf ("/ n");} return 0;}

转载请注明原文地址:https://www.9cbs.com/read-49621.html

New Post(0)