C++ : 对一个字符串按照特定字符切割

 对一个字符串(::192:168:ABC::416)按照特定字符(:)切割

#include 
#include 
#include
#include
#include
using namespace std;
vector split1(string str,char del) //标识出“空位”(::192:168:ABC::416)->(# 192 168 ABC # 416)
{
    stringstream ss(str);
    string tok;
    bool falg=true;
    vector ret;
    while(getline(ss,tok,del))
    {
        if(*str.begin() == del && falg)
        {
            falg = false;
            continue;
        }
        if(tok == "")
        {
            ret.push_back("#");
        }
        else
            ret.push_back(tok);
    }
    return ret;
};
vector split2(string str,char del) //忽略“空位”(::192:168:ABC::416)->(192 168 ABC 416)
{
    stringstream ss(str);
    string tok;
    vector ret;
    while(getline(ss,tok,del))
    {
        if(tok > "")
            ret.push_back(tok);
    }
    return ret;
};
int main()
{
    string txt = "::192:168:ABC::416";
    cout< str1 = split1(txt,':');
    for(int i=0;i str2 = split2(txt,':');
    for(int i=0;i

输出:
::192:168:ABC::416
# 192 168 ABC # 416      //标识出“空位“位置,标记(”#“)也可以用其他符号;
192 168 ABC 416            //不标识”空位“直接提取非空段子字符串

你可能感兴趣的:(c++)