posts - 11,  comments - 12,  trackbacks - 0
这是从网上看到的源代码:(感觉太麻烦,请问哪位大哥能指教我一个简单一点全面一点的分割方法吗?十分期待)
#include <vector>
#include 
<string>
#include 
<iostream>
#include 
<algorithm>
using namespace std;

typedef basic_string
<char>::size_type S_T;  
static const S_T npos = -1;  

////trim指示是否保留空串,默认为保留。tok可以为任意多个字符
vector<string> tokenize(const string& src, string tok,            
                        
bool trim=falsestring null_subst="")  
{  
    
if( src.empty() || tok.empty() ) 
        
throw "tokenize: empty string\0";  
        
    vector
<string> 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(src.substr(pre_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;  
}
 
//delimit为一个字符,严格分割
vector<string> split(const string& src, string delimit, string null_subst="")  
{  
    
if( src.empty() || delimit.empty() ) 
        
throw "split:empty string\0";    
    vector
<string> 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;  
}
   

//测试如下:
int main(int argc, char* argv[])
{
    
string src = ",ab,cde;,,fg,," ;  
    
string tok = ",;" ;   
    vector
<string> v1 = tokenize(src, tok ,true);  
    vector
<string> v2 = tokenize(src, tok ,false,     
        
"<null>");    
    cout
<<"-------------v1:"<<endl;  
    
for(int i=0; i<v1.size();i++)  
    
{  
        cout
<<v1[i]<<endl;  
    }
   
    cout
<<"-------------v2:"<<endl;  
    
for(int j=0; j<v2.size();j++)  
    
{  
        cout
<<v2[j].c_str()<<endl;  
    }
  
    
try{  
        
        
string s = "1;2;3;4";  
        
string del = ";";//"###";  
        vector<string> v3 = split(s, del, "<null>");  
        cout
<<"-------------v3:"<<endl;  
        
for(int k=0; k<v3.size();k++)  
        
{  
            cout
<<v3[k].c_str()<<endl;  
        }
  
    }
  
    
catch (char *s) {  
        cout
<<s<<endl;  
    }

    
return 0;
}

posted on 2009-08-07 14:24 人生在于攀登! 阅读(2896) 评论(3)  编辑 收藏 引用

FeedBack:
# re: 求标准C++字符串string分割
2009-08-07 22:35 | Sunshine Alike
日志里有代码功能,为啥不用?
你这样的东西放在上面没人愿意看了  回复  更多评论
  
# re: 求标准C++字符串string分割
2009-08-09 07:18 | 人生在于攀登!
哦,谢谢提醒啊,以前没有注意到!@Sunshine Alike
  回复  更多评论
  
# re: 求标准C++字符串string分割[未登录]

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年3月>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

常用链接

留言簿

随笔档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜