String Replace

ƯÁ¤ ¹®ÀÚ¿­À» ã¾Æ¼­ ¹®ÀÚ¿­À» ´ëüÇÏ´Â replace¿¡ ´ëÇؼ­ ¾Ë¾Æº¸ÀÚ.
str.replace(str.find(str2),str2.length(),"")  "abc" ¹®ÀÚ¿­À» ã¾Æ¼­ ""¹®ÀÚ¿­·Î ´ëüÇÏ°í ÀÖ´Ù.
´ÜÁö find ¸í·ÉÀ» »ç¿ëÇÏ¿© replace ¸í·ÉÀ» »ç¿ëÇÏ Ã³À½ ³ª¿À´Â ¹®ÀÚ¿­¸¸ ´ëüÇÑ´Ù.

    string str = "Hello@world";
    string tok = "@";
    string::size_type pos = str.find_first_of(tok);
    if (pos == std::string::npos)
        cout << "Position of find_first_of std::string::npos " << endl;
    else
    {
        cout << "Position of find_first_of is " << pos << endl;
        cout << "Position of find_first_of is " << str.substr(0, pos) << endl;
        str.replace(pos, tok.length(), "---");
        cout << "replace result " << str << endl;
    }

°á°ú)
Position of find_first_of is 5
Position of find_first_of is Hello
replace result Hello---world

find¿Í find_first_of Â÷ÀÌÁ¡ :
find¿¡ ¸Å°³º¯¼ö°¡ ""(ºó ¹®ÀÚ¿­)ÀÌ µé¾î°¡¸é pos°ªÀÌ 0ÀÌ´Ù.
find_first_of¿¡ ¸Å°³º¯¼ö°¡ ""(ºó ¹®ÀÚ¿­)ÀÌ µé¾î°¡¸é pos°ªÀÌ nposÀÌ´Ù.

¹®ÀÚ¿­ ´ëü¸¦ ÇϱâÀ§ÇØ ÇÊ¿äÇÑ ·ÎÁ÷À» Á¤¸®ÇÏ¸é ´ÙÀ½°ú °°ÀÌ ÇÑ´Ù.
    string str = "Hello@world";
    string tok = "@";
    string::size_type pos = str.find_first_of(tok);
    if (pos != std::string::npos)
    {
        str.replace(pos, tok.length(), "---");
        cout << "replace result " << str << endl;
    }

¸ðµç ¹®ÀÚ¿­À» ´ëü ÇÒ·Á¸é ´ÙÀ½°ú °°ÀÌ ÇÑ´Ù.

std::string ReplaceAll(std::string &str, const std::string& from, const std::string& to)
{
    size_t start_pos = 0;
    while ((start_pos = str.find(from, start_pos)) != std::string::npos) 
    {
        str.replace(start_pos, from.length(), to);
        start_pos += to.length();
    }
    return str;
}

using namespace std;

int main()
{
    string str = "Number Of Beans";
    cout << ReplaceAll(str = string("Number Of Beans"), string(" "), string("_")) << endl;
    cout << ReplaceAll(str = string("abcdabcd"), string("ab"), string("XX")) << endl;
    cout << ReplaceAll(str = string("abcdabcd"), string("ab"), string("1")) << endl;

    return 0;
}

°á°ú)
Number_Of_Beans
XXcdXXcd
1cd1cd

Âü°í)
https://hashcode.co.kr/questions/239/½ºÆ®¸µ¿¡¼­-ƯÁ¤-´Ü¾î-±³Ã¼Çϱâ