MFC CString字符串/C++ string split

来源:互联网 发布:化身孤岛的鲸 知乎 编辑:程序博客网 时间:2024/06/06 09:36

http://406625590.blog.163.com/blog/static/33530597201211307847427/


VC++ 库中, 似乎并没有特别好的 SPLIT 字符串拆分函数.  现在自己定义一个吧.

编译环境: VS 2012, UNICODE

#include <vector>

vector<CString> split(CString &str,const CString find, int limit=0) //这后面的 limit 可以指定拆分字符串的次数哦.
{
vector<CString> ret;
int start = 0;
int pos = str.Find(find,start);
int len = find.GetLength();
int i = 0;
while(true){
if(pos<0 || (limit>0 && i+1==limit)){ //NO FIND
ret.push_back(str.Mid(start));
break;
}else{
ret.push_back(str.Mid(start,pos-start));
start = pos+len;
pos = str.Find(find,start);
}
i++;
}
return ret;
}


应用示例:
----------------------------

CString t = L"a,.b,.c,de.f";
vector<CString> tData = split(t,L"."); //这里的分隔符是支持多个字符串的哦. 
CString out = L"";
for(vector<CString>::iterator iter = tData.begin(); iter!=tData.end(); ++iter){
//this->MessageBox(*iter);
out += *iter;
out += L"\r\n";
out += L"-----------------";
out += L"\r\n";
}
this->MessageBox(out);

C++ string的trim, split方法

http://blog.csdn.net/butterfly_dreaming/article/details/10142443


字符串分割(C++)

http://www.cnblogs.com/MikeZhang/archive/2012/03/24/MySplitFunCPP.html


C++中如何split字符串

 

http://blog.sina.com.cn/s/blog_48d4cf2d0100r9hv.html

0 0
原创粉丝点击