例:提取在字符'/'之间的字符串

如:

/articles/2003/ 
/articles/<int>/
/articles/<int>/<int>/ 
/articles/<int>/<int>/<str>/ 
/static/<path> 

方法一:使用string的find函数和substr函数来提取子串

vector<string> vs[N];

 

void splitString(string st, int i)
{
    string ss;
    int len = st.length();  //字符串总长度
    size_t j = 0, k = 0;
    while (k < len - 1)
    {
        k = st.find_first_of("/", j + 1);
        if (k == string::npos)
            k = len;
        ss = st.substr(j + 1, k - j - 1);  //使用substr函数提取子串
        vs[i].push_back(ss);
        j = k;
    }
}

方法二:使用stringstream这个类,然后用getline方法提取 

void splitString(string st, int i)
{
    stringstream ss(st);
    string sn;
    while (getline(ss, sn, '/')) //存入字符串sn内,遇到字符'/'结束
    {
        if (sn == "/"||sn=="") //这里需要增加判断sn=="",否则会多空行
            continue;
        vs[i].push_back(sn);
    }
}

输入: