xuejianhui

导航

C++ : 自写两个字符串分割函数(含测试程序)

  1 // 文件名 : SplitStrng.h
  2 // 作用   : 分割字符串
  3 #include <vector>
  4 #include <string>
  5 #include <iostream>
  6 #include <algorithm>
  7 using namespace std;
  8 
  9 typedef basic_string<char>::size_type S_T;
 10 static const S_T npos = -1;
 11 
 12 // split        : 根据分割符严格按照分割符进行分割
 13 // src          : 原始字符串
 14 // delimit      : 为一个字符,严格分割
 15 // null_subst   : 将空串替换为任意多个字符
 16 // 返回分割后的字符串数组
 17 vector<string> split(const string& src, string delimit, string null_subst="")
 18 {
 19     if( src.empty() || delimit.empty() )
 20     {
 21         throw "split:empty string\0";
 22     }
 23 
 24     vector<string> v;
 25     S_T deli_len = delimit.size();
 26     long index = npos, last_search_position = 0;
 27 
 28     while( (index = src.find(delimit, last_search_position)) != npos )
 29     {
 30         if(index == last_search_position)
 31         {
 32             v.push_back(null_subst);
 33         }
 34         else
 35         {
 36             v.push_back( src.substr(last_search_position, 
 37                                 index - last_search_position) );
 38         }
 39 
 40         last_search_position = index + deli_len;  
 41     }
 42 
 43     string last_one = src.substr(last_search_position);
 44     v.push_back( last_one.empty()? null_subst:last_one );
 45     return v;
 46 }
 47 
 48 // tokenize     : 根据要过滤的字符分割字符串,并可以选择是否显示空串或替换空串
 49 // src          : 原始字符串
 50 // tok          : 将要过滤掉的字符(如:";," 表示将要过滤掉所有';'和','字符)
 51 // trim         : 指示是否保留空串,默认为保留。
 52 // null_subst   : 将空串替换为任意多个字符
 53 // 返回分割后的字符串数组
 54 vector<string> tokenize(const string& src, 
 55                         string tok, 
 56                         bool trim = false, 
 57                         string null_subst = "")
 58 {  
 59     if( src.empty() || tok.empty() )
 60     {
 61         throw "tokenize: empty string\0";  
 62     }
 63 
 64     vector<string> v;  
 65     S_T pre_index = 0, index = 0, len = 0;
 66     while( (index = src.find_first_of(tok, pre_index)) !=npos )  
 67     {  
 68         if( (len = index-pre_index)!=0 )
 69         {
 70             v.push_back(src.substr(pre_index, len));
 71         }
 72         else if(trim == false)
 73         {
 74             v.push_back(null_subst);
 75         }
 76 
 77         pre_index = index+1;
 78     }
 79 
 80     string endstr = src.substr(pre_index);
 81     if( trim == false )
 82     {
 83         v.push_back( endstr.empty()?null_subst:endstr );
 84     }
 85     else if( !endstr.empty() )
 86     {
 87         v.push_back(endstr);
 88     }
 89 
 90     return v;  
 91 }
 92 
 93 //##############################################################################
 94 
 95 //测试程序如下:
 96 void SplitStringTest(void)
 97 {
 98     //----tokenize----test------------------------------------------------------
 99 
100     string src = ",ab,cde;,fg,,";
101     string tok = ";,";
102 
103     vector<string> v1 = tokenize(src, tok , true);
104     vector<string> v2 = tokenize(src, tok , false, "<null>");
105 
106     for(int i = 0; i < v1.size(); i++)
107     {
108         cout<< "szTmpV1" <<endl;
109         string szTmpV1 = v1[i].c_str();
110         cout<< szTmpV1 <<endl;
111     }
112 
113     for(int j = 0; j < v2.size(); j++)
114     {
115         cout<< "szTmpV2" <<endl;
116         string szTmpV2 = v2[j].c_str();
117         cout<< szTmpV2 <<endl;
118     }
119 
120     //----split----test---------------------------------------------------------
121 
122     string s = "1\\2\\3\\4";
123     string del = "\\";
124 
125     try
126     {
127         vector<string> v3 = split(s, del, "<null>");
128 
129         for(UINT k = 0; k < v3.size(); k++)
130         {
131             cout<< "szTmp" <<endl;
132             string szTmp = v3[k].c_str();
133             cout<< szTmp <<endl;
134         }
135     }
136     catch (char *s)
137     {
138         cout<<s<<endl;
139     }
140 
141     return;
142 }

posted on 2012-10-10 17:50  xuejianhui  阅读(314)  评论(0编辑  收藏  举报