C++ 字符串分割

#include <vcl.h>
#include <string>
#include <vector>
#include <iostream>
#pragma hdrstop

#pragma argsused

using namespace std;

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

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);
}

vector<String> splitUnicodeString( String & src, String delimit )
{
vector<String>
resultVec;
vector<string>
strVec = split( src.c_str(), delimit.c_str() );
for ( int i = 0; i < strVec.size(); i++ )
{
String str( strVec[i].c_str() );
resultVec.push_back( str );
}
return(resultVec);
}

int _tmain( int argc, _TCHAR* argv[] )
{
String
s = "a,bcd,efg";
String
del = ",";
vector<String>
strVec = splitUnicodeString( s, del );
for ( int i = 0; i < strVec.size(); i++ )
{
ShowMessage( strVec[i] );
}

return(0);
}

原文链接: https://www.cnblogs.com/jerry1999/archive/2013/05/08/3677344.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    C++ 字符串分割

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/87619

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 下午11:11
下一篇 2023年2月9日 下午11:11

相关推荐