C++ replace() 函数用法详解
2014-04-18 16:15
821 查看
文主要针对c++中常用replace函数用法给出样例程序
[cpp]
view plaincopy
/*用法一:
*用str替换指定字符串从起始位置pos开始长度为len的字符
*string& replace (size_t pos, size_t len, const string& str);
*/
int main()
{
string line = "this@ is@ a test string!";
line = line.replace(line.find("@"), 1, ""); //从第一个@位置替换第一个@为空
cout << line << endl;
return 0;
}
运行结果:
[cpp]
view plaincopy
/*用法二:
*用str替换 迭代器起始位置 和 结束位置 的字符
*string& replace (const_iterator i1, const_iterator i2, const string& str);
*/
int main()
{
string line = "this@ is@ a test string!";
line = line.replace(line.begin(), line.begin()+6, ""); //用str替换从begin位置开始的6个字符
cout << line << endl;
return 0;
}
运行结果:
[cpp]
view plaincopy
/*用法三:
*用substr的指定子串(给定起始位置和长度)替换从指定位置上的字符串
*string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen);
*/
int main()
{
string line = "this@ is@ a test string!";
string substr = "12345";
line = line.replace(0, 5, substr, substr.find("1"), 3); //用substr的指定子串(从1位置数共3个字符)替换从0到5位置上的line
cout << line << endl;
return 0;
}
运行结果:
[cpp]
view plaincopy
/*用法四:string转char*时编译器可能会报出警告,不建议这样做
*用str替换从指定位置0开始长度为5的字符串
*string& replace(size_t pos, size_t len, const char* s);
*/
int main()
{
string line = "this@ is@ a test string!";
char* str = "12345";
line = line.replace(0, 5, str); //用str替换从指定位置0开始长度为5的字符串
cout << line << endl;
return 0;
}
运行结果:
[cpp]
view plaincopy
/*用法五:string转char*时编译器可能会报出警告,不建议这样做
*用str替换从指定迭代器位置的字符串
*string& replace (const_iterator i1, const_iterator i2, const char* s);
*/
int main()
{
string line = "this@ is@ a test string!";
char* str = "12345";
line = line.replace(line.begin(), line.begin()+9, str); //用str替换从指定迭代器位置的字符串
cout << line << endl;
return 0;
}
运行结果:
[cpp]
view plaincopy
/*用法六:string转char*时编译器可能会报出警告,不建议这样做
*用s的前n个字符替换从开始位置pos长度为len的字符串
*string& replace(size_t pos, size_t len, const char* s, size_t n);
*/
int main()
{
string line = "this@ is@ a test string!";
char* str = "12345";
line = line.replace(0, 9, str, 4); //用str的前4个字符替换从0位置开始长度为9的字符串
cout << line << endl;
return 0;
}
运行结果:
[cpp]
view plaincopy
/*用法七:string转char*时编译器可能会报出警告,不建议这样做
*用s的前n个字符替换指定迭代器位置(从i1到i2)的字符串
*string& replace (const_iterator i1, const_iterator i2, const char* s, size_t n);
*/
int main()
{
string line = "this@ is@ a test string!";
char* str = "12345";
line = line.replace(line.begin(), line.begin()+9, str, 4); //用str的前4个字符替换指定迭代器位置的字符串
cout << line << endl;
return 0;
}
运行结果:
[cpp]
view plaincopy
/*用法八:
*用重复n次的c字符替换从指定位置pos长度为len的内容
*string& replace (size_t pos, size_t len, size_t n, char c);
*/
int main()
{
string line = "this@ is@ a test string!";
char c = '1';
line = line.replace(0, 9, 3, c); //用重复3次的c字符替换从指定位置0长度为9的内容
cout << line << endl;
return 0;
}
运行结果:
[cpp]
view plaincopy
/*用法九:
*用重复n次的c字符替换从指定迭代器位置(从i1开始到结束)的内容
*string& replace (const_iterator i1, const_iterator i2, size_t n, char c);
*/
int main()
{
string line = "this@ is@ a test string!";
char c = '1';
line = line.replace(line.begin(), line.begin()+9, 3, c); //用重复3次的c字符替换从指定迭代器位置的内容
cout << line << endl;
return 0;
}
运行结果:
注:所有使用迭代器类型的参数不限于string类型,可以为vector、list等其他类型迭代器。
[cpp]
view plaincopy
/*用法一:
*用str替换指定字符串从起始位置pos开始长度为len的字符
*string& replace (size_t pos, size_t len, const string& str);
*/
int main()
{
string line = "this@ is@ a test string!";
line = line.replace(line.find("@"), 1, ""); //从第一个@位置替换第一个@为空
cout << line << endl;
return 0;
}
运行结果:
[cpp]
view plaincopy
/*用法二:
*用str替换 迭代器起始位置 和 结束位置 的字符
*string& replace (const_iterator i1, const_iterator i2, const string& str);
*/
int main()
{
string line = "this@ is@ a test string!";
line = line.replace(line.begin(), line.begin()+6, ""); //用str替换从begin位置开始的6个字符
cout << line << endl;
return 0;
}
运行结果:
[cpp]
view plaincopy
/*用法三:
*用substr的指定子串(给定起始位置和长度)替换从指定位置上的字符串
*string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen);
*/
int main()
{
string line = "this@ is@ a test string!";
string substr = "12345";
line = line.replace(0, 5, substr, substr.find("1"), 3); //用substr的指定子串(从1位置数共3个字符)替换从0到5位置上的line
cout << line << endl;
return 0;
}
运行结果:
[cpp]
view plaincopy
/*用法四:string转char*时编译器可能会报出警告,不建议这样做
*用str替换从指定位置0开始长度为5的字符串
*string& replace(size_t pos, size_t len, const char* s);
*/
int main()
{
string line = "this@ is@ a test string!";
char* str = "12345";
line = line.replace(0, 5, str); //用str替换从指定位置0开始长度为5的字符串
cout << line << endl;
return 0;
}
运行结果:
[cpp]
view plaincopy
/*用法五:string转char*时编译器可能会报出警告,不建议这样做
*用str替换从指定迭代器位置的字符串
*string& replace (const_iterator i1, const_iterator i2, const char* s);
*/
int main()
{
string line = "this@ is@ a test string!";
char* str = "12345";
line = line.replace(line.begin(), line.begin()+9, str); //用str替换从指定迭代器位置的字符串
cout << line << endl;
return 0;
}
运行结果:
[cpp]
view plaincopy
/*用法六:string转char*时编译器可能会报出警告,不建议这样做
*用s的前n个字符替换从开始位置pos长度为len的字符串
*string& replace(size_t pos, size_t len, const char* s, size_t n);
*/
int main()
{
string line = "this@ is@ a test string!";
char* str = "12345";
line = line.replace(0, 9, str, 4); //用str的前4个字符替换从0位置开始长度为9的字符串
cout << line << endl;
return 0;
}
运行结果:
[cpp]
view plaincopy
/*用法七:string转char*时编译器可能会报出警告,不建议这样做
*用s的前n个字符替换指定迭代器位置(从i1到i2)的字符串
*string& replace (const_iterator i1, const_iterator i2, const char* s, size_t n);
*/
int main()
{
string line = "this@ is@ a test string!";
char* str = "12345";
line = line.replace(line.begin(), line.begin()+9, str, 4); //用str的前4个字符替换指定迭代器位置的字符串
cout << line << endl;
return 0;
}
运行结果:
[cpp]
view plaincopy
/*用法八:
*用重复n次的c字符替换从指定位置pos长度为len的内容
*string& replace (size_t pos, size_t len, size_t n, char c);
*/
int main()
{
string line = "this@ is@ a test string!";
char c = '1';
line = line.replace(0, 9, 3, c); //用重复3次的c字符替换从指定位置0长度为9的内容
cout << line << endl;
return 0;
}
运行结果:
[cpp]
view plaincopy
/*用法九:
*用重复n次的c字符替换从指定迭代器位置(从i1开始到结束)的内容
*string& replace (const_iterator i1, const_iterator i2, size_t n, char c);
*/
int main()
{
string line = "this@ is@ a test string!";
char c = '1';
line = line.replace(line.begin(), line.begin()+9, 3, c); //用重复3次的c字符替换从指定迭代器位置的内容
cout << line << endl;
return 0;
}
运行结果:
注:所有使用迭代器类型的参数不限于string类型,可以为vector、list等其他类型迭代器。
相关文章推荐
- C++ STL replace()函数常用用法详解
- C++学习之const成员函数(函数声明后加const,或称常量成员函数)用法详解
- C++中的const成员函数(函数声明后加const,或称常量成员函数)用法详解
- [C/C++] printf函数用法详解
- 详解C++成员函数的override和final说明符的用法
- c++ 标准模板库 STL 算法之 for_each 函数的使用用法详解
- c++: string 的函数replace()用法
- C++中的const成员函数(函数声明后加const,或称常量成员函数)用法详解
- C++中memset()函数的用法详解
- C++中的const成员函数(函数声明后加const,或称常量成员函数)用法详解
- c++ 标准模板库 STL 算法之 for_each 函数的使用用法详解
- PHP字符串替换str_replace()函数4种用法详解
- C++ STL replace()函数常用用法详解
- PHP字符串替换str_replace()函数4种用法详解
- C++中的const成员函数(函数声明后加const,或称常量成员函数)用法详解
- Boost.Bind用法详解(一) 2008-05-09 15:50:50| 分类: C++ |字号 订阅 Boost.Bind 为函数和函数对象提供了一致的语法,对于值语义和指针语义也一样。
- C++中的const成员函数(函数声明后加const,或称常量成员函数)用法详解
- C++中的const成员函数(函数声明后加const,或称常量成员函数)用法详解
- c++中vector的用法详解-函数实现
- C++中的const成员函数(函数声明后加const,或称常量成员函数)用法详解