zoukankan      html  css  js  c++  java
  • 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);
    }
  • 相关阅读:
    java 枚举类小结 Enum
    hibernate查询
    java装饰者模式理解
    内部类学习
    java使用json将HashMap转化成javabean小例子
    HashMap存储数据赋值javabean简单示例
    java数组转化成集合
    java正则匹配并提取字串
    Windows cmd命令反斜杠问题
    自动化构建工具
  • 原文地址:https://www.cnblogs.com/jerry1999/p/3677344.html
Copyright © 2011-2022 走看看