STL str replace

#include <iostream>
#include <string>
using namespace std;

void main()
{
    string s="Hello Lucy!";
    //s.replace(5,1,"Lily");
    //"Lucy" -> "Lily"
    int indexStart=s.find("Lucy");
    string l("Lily");
    int len=l.length();
    if (indexStart>=0)
    {
        s.replace(indexStart,len,"Lily");
    }

    cout<<s<<endl;
}
// 
// Hello Lily!
// Press any key to continue

 

#include <iostream>
#include <string>
using namespace std;

void main()
{
    string s="Hello Lucy!I am Jim";
    //s.replace(5,1,"Lily");
    //"Lucy" -> "Lily"
    string l("Jim");
    int indexStart=s.find(l);    
    int len=l.length();
    if (indexStart>=0)
    {
        s.replace(indexStart,len,"Lily");
    }

    cout<<s<<endl;
}
// Hello Lucy!I am Lily
// Press any key to continue

 

posted @ 2017-02-14 09:17  sky20080101  阅读(225)  评论(0编辑  收藏  举报