p12_2.cpp

来自「相当丰富的C++源码」· C++ 代码 · 共 26 行

CPP
26
字号
/***********************************************
*   p12_2.cpp                                  *
*   string类的成员函数的使用,字符串查找与替换 *
***********************************************/
#include <string>
#include <iostream>
using namespace std;
void main()
{
	string text("I like c++, "
		        "I use c++ programming."); //文本
	string newstr;                        //新串
	string sstr;                          //待查串
	int pos;                              //存放查找到串的位置
	cout<<"Input string and new string:";
	cin>>sstr>>newstr;
	if((pos=text.find(sstr))==string::npos) //未查找到
		cout<<sstr<<" not found in \""<<text<<"\""<<endl;
	else {
        cout<<"old string: "<<text<<endl;
	    text.replace(pos,sstr.length(),newstr);
		cout<<"new string: "<<text<<endl;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?