分割字符串,字符串去除空格

 开始

```
find() : Finds the first substring equal to the given character sequence
find_first_of() : Finds the first character equal to one of characters in the given character sequence
//字符串分割函数 str为待分割的字符串 pattern为任意分割符(**每一个字符都为分割符**),
//例如:";,"是以 ';' 和 ',' 分割,并且去掉了分割后的空字符串。 sbcssdfs 以s分割得 "bc" "df"
vector<string> split(string str, string pattern)
{
    vector<string> ret;
    if (pattern.empty()) return ret;
    size_t start = 0;
    size_t index = str.find_first_of(pattern, 0);
    while (index != string::npos)
    {
        if (start != index)
            ret.push_back(str.substr(start, index - start));
        start = index + 1;
        index = str.find_first_of(pattern, start);
    }
 
    if (!str.substr(start).empty())
        ret.push_back(str.substr(start));
    return ret;
}
//分割后保留空字符串。sbcssdfs 以s分割得 "" "bc" "" "df" ""
vector<string> split(string str, string pattern)
{
    vector<string> ret;
    if (pattern.empty()) return ret;
    size_t start = 0;
    size_t index = str.find_first_of(pattern, 0);
    while (index != string::npos)
    {
        ret.push_back(str.substr(start, index - start));
        start = index + 1;
        index = str.find_first_of(pattern, start);
    }
    ret.push_back(str.substr(start));
    return ret;
}
```
用Poco中的StringTokenizer
```
#include "Poco/StringTokenizer.h"
#include "Poco/String.h" // for cat
// 分割字符串,s:将被分割的字符串,k:分割符
// 例如: k是 ";,"表示用 ';' 和 ',' 分割
std::vector<std::string> SplitString(std::string s, std::string k)
{
    /*
    Poco::StringTokenizer t1("red, green, blue", ",");
    // "red", " green", " blue"     (note the whitespace)
    Poco::StringTokenizer t2("red,green,,blue", ",");
    // "red", "green", "", "blue"
    Poco::StringTokenizer t3("red ; green , blue", ",;",
        Poco::StringTokenizer::TOK_TRIM);
    // "red", "green", "blue"
    Poco::StringTokenizer t4("red; green,, blue", ",;",
        Poco::StringTokenizer::TOK_TRIM | Poco::StringTokenizer::TOK_IGNORE_EMPTY);
    // "red", "green", "blue"
    std::string ss(Poco::cat(std::string("; "), t4.begin(), t4.end()));
    // "red; green; blue
    */
    Poco::StringTokenizer st(s, k);//这样是不去掉 "" ,当加上 TOK_IGNORE_EMPTY(宏定义为1)即去掉 "" 了。
    std::vector<std::string> vecRet(st.begin(), st.end());
    return vecRet;
}
```
另外一个字符串整体为分割符:(单个字符就跟上面的一样了)
```
//pattern作为一个整体当分割符,(不去掉 "")
vector<string> split(string str, string pattern)
{
    vector<string> ret;
    if (pattern.empty()) return ret;
    size_t start = 0;
    size_t index = str.find(pattern, 0);
    while (index != string::npos)
    {
        ret.push_back(str.substr(start, index - start));
        start = index + pattern.size();
        index = str.find(pattern, start);
    }
    ret.push_back(str.substr(start));
    return ret;
}
```

 

另外,有strtok

char *strtok(char s[], const char *delim);
wchar_t *wcstok(wchar_t s[], const wchar_t *delim)
_tcstok() 对应 TCHAR 的函数
分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。strtok函数会破坏被分解字符串的完整,调用前和调用后的s已经不一样了。
#pragma warning (disable : 4996)
int main()
{
    char s[] = "this is. 192...168.0.1..";
    char *token = strtok(s, " .");//以空格和“.”进行分割,并且不保留空字符串
    while (token)
    {
        cout << token << '\n';
        token = strtok(NULL, " .");
        //_tcstok()
    }
    return 0;
}//结果: this is 192 168 0 1

用其实现的split函数

// delim分隔符,所有delim中包含的字符都是分隔符,且不保留空字符串""
#pragma warning (disable : 4996)
std::vector<std::string> split(char* s, const char* delim)
{
    std::vector<std::string> vecRet;
    char* token = strtok(s, delim);
    while (token)
    {
        vecRet.push_back(std::string(token));
        token = strtok(NULL, delim);
    }
    return vecRet;
}

std::vector<CString> split(TCHAR* s, const TCHAR* delim)
{
    std::vector<CString> vecRet;
    TCHAR* token = _tcstok(s, delim);
    while (token)
    {
        vecRet.push_back(CString(token));
        token = _tcstok(NULL, delim);
    }
    return vecRet;
}
View Code

 

还有 CString的 Tokenize()

原型:
CStringT Tokenize( PCXSTR pszTokens , int& iStart ) const;
功能:
从iStart位置取出字符串中含pszTokens分割符间的内容;
参数
pszTokens:包含标记分隔符的字符串。 这些分隔符顺序并不重要。
iStart:开始搜索的从零开始的索引。
返回值
包含当前标记值的 CStringT 对象。
备注 :
将 pszTokens 的字符指定将找到的标记可能的分隔符。 在每次调用Tokenize函数开始对 iStart,导致分隔符的"跳过",并返回包含当前标记的CStringT对 象,是字符串到下分隔符。如果该字符串的末尾已到达,更新 iStart 的值为后面的位置关闭分隔符的或-1。 多个标记中打开目标字符串的其余部分由一系列调用Tokenize,使用 iStart 记录在字符串下一个标记要读取的位置。 当没有更多的标记函数返回一个空字符串,并 iStart 将设置为-1。 Tokenize不修改目标字符串。
// delim分隔符,所有delim中包含的字符都是分隔符,且不保留空字符串""
std::vector<CString> split(const CString& str, const CString& delim)
{
    std::vector<CString> vecRet;
    const int nLen = str.GetLength();
    int pos = 0;
    CString s;
    while ((s = str.Tokenize(delim, pos)) != _T(""))
    {
        vecRet.push_back(s);
    }
    return vecRet;
}

 使用C语言风格的字符串函数,<string.h>或<cstring>

 char* strstr(const char* p, const char* q)
在字符串p中查找第一次出现字符串q的位置,返回指向他的指针,否则返回NULL
char* strpbrk(const char* p, const char* p1)
在p指向的字符串中查找第一次出现p1指向的字符串中任何字符的位置。成功返回p中指向该字符的指针,失败返回NULL。

// delim分隔符,所有delim中包含的字符都是分隔符,保留空字符串"",前向分割
void split(const char* str, const char* delim, std::vector<std::string> &vRet)
{
    const char* p = str;
    const char* q = NULL;
    while (*p && (q = strpbrk(p, delim)))
    {
        vRet.push_back(std::string(p, q));
        ++q;
        p = q;
    }
    if (*p)
        vRet.push_back(std::string(p));
}

int main()
{
    char s[] = "...this is. 192..";
    char d[] = ". ";
    std::vector<std::string> vRet;
    split(s, d, vRet);//Result: "" "" "" "this" "is" "" "192" ""
    return 0;
}

 

// str是待分割的字符串 delim整体作为一个分隔符,保留空字符串"",前向分割
void split(const char* str, const char* delim, std::vector<std::string> &vRet)
{
    const int n = strlen(delim);
    const char* p = str;
    const char* q = NULL;
    while (*p && (q = strstr(p, delim)))
    {
        vRet.push_back(std::string(p, q));
        q = q + n;
        p = q;
    }
    if (*p)
        vRet.push_back(std::string(p));
}

int main()
{
    char s[] = "...this is. 192..";
    char d[] = ". ";
    std::vector<std::string> vRet;
    split(s, d, vRet);//Result: "...this is" "192.."
    return 0;
}

 

字符串去除空格

void eatspaces(char* s)
{
    int i = 0;
    int j = 0;
    while ((s[i] = s[j]) != '\0')
    {
        ++j;
        if (s[i] != ' ')
            ++i;
    }
}

 

 

--------------------------------------  2020-10-17  -------------------------------------

//---------------------------------------------------------------------------------------
// 功能:分割字符串,保留空字符串"",前向分割。(char* 版)
// 参数:str 待分割的字符串,delim 分隔符(所有delim中包含的每个字符都是分隔符),vRet 返回分割后的结果
// 例如:"0hello world0.00", " 0" --> "" "hello" "world" "." ""
//---------------------------------------------------------------------------------------
void split1(const char* str, const char* delim, std::vector<std::string> &vRet)
{
    if (NULL == str || NULL == delim || '\0' == *str || '\0' == *delim)
        return;
    if (NULL == strpbrk(str, delim))//若一个都delim没有查到,就直接返回
        return;
    const int nLen = strlen(str);
    const char* p = str;
    const char* q = NULL;
    while (p < str + nLen)
    {
        q = strpbrk(p, delim);
        if (q != NULL)
        {
            vRet.push_back(std::string(p, q));
            p = q + 1;
        }
        else
        {
            vRet.push_back(std::string(p));
            break;
        }
    }
}

//---------------------------------------------------------------------------------------
// 功能:分割字符串,保留空字符串"",前向分割。(wchar_t* 版)
// 参数:str 待分割的字符串,delim 分隔符(所有delim中包含的每个字符都是分隔符),vRet 返回分割后的结果
// 例如:"0hello world0.00", " 0" --> "" "hello" "world" "." ""
//---------------------------------------------------------------------------------------
void split1(const wchar_t* str, const wchar_t* delim, std::vector<std::wstring> &vRet)
{
    if (NULL == str || NULL == delim || '\0' == *str || '\0' == *delim)
        return;
    if (NULL == wcspbrk(str, delim))//若一个都delim没有查到,就直接返回
        return;
    const int nLen = wcslen(str);
    const wchar_t* p = str;
    const wchar_t* q = NULL;
    while (p < str + nLen)
    {
        q = wcspbrk(p, delim);
        if (q != NULL)
        {
            vRet.push_back(std::wstring(p, q));
            p = q + 1;
        }
        else
        {
            vRet.push_back(std::wstring(p));
            break;
        }
    }
}


//---------------------------------------------------------------------------------------
// 功能:分割字符串(整体作为一个分隔符),保留空字符串"",前向分割。(char* 版)
// 参数:str是待分割的字符串 delim整体作为一个分隔符,vRet 返回分割后的结果
// 例如:"00hello00world0.00", "00" --> "" "hello" "world0."
//---------------------------------------------------------------------------------------
void split2(const char* str, const char* delim, std::vector<std::string> &vRet)
{
    if (NULL == str || NULL == delim || '\0' == *str || '\0' == *delim)
        return;
    if (NULL == strstr(str, delim))//若一个都delim没有查到,就直接返回
        return;
    const int nLen = strlen(str);
    const int n = strlen(delim);
    const char* p = str;
    const char* q = NULL;
    while (p < str + nLen)
    {
        q = strstr(p, delim);
        if (q != NULL)
        {
            vRet.push_back(std::string(p, q));
            p = q + n;
        }
        else
        {
            vRet.push_back(std::string(p));
            break;
        }
    }
}

//---------------------------------------------------------------------------------------
// 功能:分割字符串(整体作为一个分隔符),保留空字符串"",前向分割。(wchar_t* 版)
// 参数:str是待分割的字符串 delim整体作为一个分隔符,vRet 返回分割后的结果
// 例如:"00hello00world0.00", "00" --> "" "hello" "world0."
//---------------------------------------------------------------------------------------
void split2(const wchar_t* str, const wchar_t* delim, std::vector<std::wstring> &vRet)
{
    if (NULL == str || NULL == delim || '\0' == *str || '\0' == *delim)
        return;
    if (NULL == wcsstr(str, delim))//若一个都delim没有查到,就直接返回
        return;
    const int nLen = wcslen(str);
    const int n = wcslen(delim);
    const wchar_t* p = str;
    const wchar_t* q = NULL;
    while (p < str + nLen)
    {
        q = wcsstr(p, delim);
        if (q != NULL)
        {
            vRet.push_back(std::wstring(p, q));
            p = q + n;
        }
        else
        {
            vRet.push_back(std::wstring(p));
            break;
        }
    }
}


//---------------------------------------------------------------------------------------
// 分割字符串str,pattern中的每个字符都是分隔符,前向分割。(std::string 版)
// 例如:"0hello world0.00", " 0" --> "" "hello" "world" "." ""
//---------------------------------------------------------------------------------------
void split1(const string& str, const string& pattern, vector<string>& ret)
{
    if (pattern.empty() || str.empty())
        return;
    size_t start = 0;
    size_t index = str.find_first_of(pattern, 0);
    while (index != string::npos)
    {
        ret.push_back(str.substr(start, index - start));
        start = index + 1;
        index = str.find_first_of(pattern, start);
    }
    if (start < str.size())
        ret.push_back(str.substr(start));
}
//---------------------------------------------------------------------------------------
// 分割字符串str,pattern中的每个字符都是分隔符,前向分割。(std::wstring 版)
// 例如:"0hello world0.00", " 0" --> "" "hello" "world" "." ""
//---------------------------------------------------------------------------------------
void split1(const std::wstring& str, const std::wstring& pattern, vector<std::wstring>& ret)
{
    if (pattern.empty() || str.empty())
        return;
    size_t start = 0;
    size_t index = str.find_first_of(pattern, 0);
    while (index != std::wstring::npos)
    {
        ret.push_back(str.substr(start, index - start));
        start = index + 1;
        index = str.find_first_of(pattern, start);
    }
    if (start < str.size())
        ret.push_back(str.substr(start));
}

//---------------------------------------------------------------------------------------
// 分割字符串str,pattern整体做为一个分隔符,前向分割。(std::string 版)
// 例如:"00hello00world0.00", "00" --> "" "hello" "world0."
//---------------------------------------------------------------------------------------
void split2(const string& str, const string& pattern, vector<string>& ret)
{
    if (pattern.empty() || str.empty())
        return;
    size_t nLen = pattern.size();
    size_t start = 0;
    size_t index = str.find(pattern, 0);
    while (index != string::npos)
    {
        ret.push_back(str.substr(start, index - start));
        start = index + nLen;
        index = str.find(pattern, start);
    }
    if (start < str.size())
        ret.push_back(str.substr(start));
}
//---------------------------------------------------------------------------------------
// 分割字符串str,pattern整体做为一个分隔符,前向分割。(std::wstring 版)
// 例如:"00hello00world0.00", "00" --> "" "hello" "world0."
//---------------------------------------------------------------------------------------
void split2(const std::wstring& str, const std::wstring& pattern, vector<std::wstring>& ret)
{
    if (pattern.empty() || str.empty())
        return;
    size_t nLen = pattern.size();
    size_t start = 0;
    size_t index = str.find(pattern, 0);
    while (index != std::wstring::npos)
    {
        ret.push_back(str.substr(start, index - start));
        start = index + nLen;
        index = str.find(pattern, start);
    }
    if (start < str.size())
        ret.push_back(str.substr(start));
}

 

posted @ 2019-08-14 20:51  htj10  阅读(1782)  评论(0编辑  收藏  举报
TOP