C++ 用字符串分割字符串
最近学习使用C++写点工具,遇到了字符串分割的问题,上网搜索了几个例程,都是采用单个字符分割字符串,不是我想要的,于是自己动手写了一个split函数: #include #include #include using namespace std; vector { vector int intLen_d = delim.length(); if (intLen_d > 0) { string::size_type pos; string strTemp; int intLen_s = strOrig.length(); for(int i = 0; i <= intLen_s; i++) { pos = strOrig.find(delim,i); if (pos > intLen_s) pos = intLen_s; strTemp = strOrig.substr(i,pos - i); vecResult.push_back(strTemp); i = pos + intLen_d - 1; } } else vecResult.push_back(strOrig); return vecResult; } int main() { string t1 = "......"; vector tx = strSplitB(t1,"..."); for(int i = 0; i < tx.size(); i++) { cout << tx[i] << endl; } return 0; } 以上代码测试用字符串“...”(3个小数点)分割字符串“......”(6个小数点)得到的向量由3个空字符组成["","",""],这就是我想要的结果。顺便提一句,这和python的split函数运行结果一致。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |