您当前的位置: 首页 >  蓝不蓝编程 c++

C++ 字符串分割

蓝不蓝编程 发布时间:2013-05-08 22:28:27 ,浏览量:6

#include
#include
#include
#include
#pragma hdrstop

#pragma argsused

using namespace std;

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

vector split( const string & src, string delimit, string null_subst = "" )
{
if ( src.empty() || delimit.empty() )
throw "split: empty string\0";

vector 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 splitUnicodeString( String & src, String delimit )
{
vector resultVec;
vector 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 strVec = splitUnicodeString( s, del );
for ( int i = 0; i < strVec.size(); i++ )
{
ShowMessage( strVec[i] );
}

return(0);
}
关注
打赏
查看更多评论

蓝不蓝编程

暂无认证

  • 6浏览

    0关注

    537博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录