C++删除字符串中指定位子或者指定字符或字符串的函数。
展开全部
C++中字符串删除函数为 std::string::erase 有三种形式:
sequence (1) string& erase (size_t pos = 0, size_t len = npos);
character (2) iterator erase (iterator p);
range (3) iterator erase (iterator first, iterator last);
示例:
// string::erase
#include <iostream>
#include <string>
int main ()
{
std::string str ("This is an example sentence.");
std::cout << str << '\n';
// "This is an example sentence."
str.erase (10,8); // (1) ^^^^^^^^
std::cout << str << '\n';
// "This is an sentence."
str.erase (str.begin()+9); // (2) ^
std::cout << str << '\n';
// "This is a sentence."
str.erase (str.begin()+5, str.end()-9); // (3) ^^^^^
std::cout << str << '\n';
// "This sentence."
return 0;
}
结果:
This is an example sentence.
This is an sentence.
This is a sentence.
This sentence.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询