본문 바로가기
Programming/MFC,C++

split replace c++

by 기적 2013. 7. 2.

출처 : http://blog.daum.net/lifemap/5474796

 

 

void trimleft(std::string& str, const char* ch)
{
    str.erase(0, str.find_first_not_of(ch));
}
void trimright(std::string& str, const char* ch)
{
    str.erase(str.find_last_not_of(ch) + 1);
}
std::vector<std::string> split(std::string& str, std::string separators )
{
    size_t n = str.length();
    size_t start, stop;
    std::vector<std::string> result;
    start = str.find_first_not_of(separators);

    while ((start >= 0) && (start < n)) {
        stop = str.find_first_of(separators, start);
        if ((stop < 0) || (stop > n)) stop = n;
        result.push_back(str.substr(start, stop - start));
        start = str.find_first_not_of(separators, stop+1);
    }
    return result;
}
void replace( std::string& text, const std::string& find_token, const std::string& replace_token )
{
   size_t i = 0;
   while ((i = text.find(find_token)) != std::string::npos)
       text.replace(i, find_token.size(), replace_token);
}


댓글