怎样删除字符串中的一个字符.该怎么解决
怎样删除字符串中的一个字符.
如char *p= "good ",怎么样把一个o给删了,如果是string str( "good ")怎么做
------解决方案--------------------
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str( "good ");
cout < < "string: " < <str < <endl;
str.erase(str.find( 'o ',0), 1);
cout < < "after delete: " < <str < <endl;
char cStr[] = "good ";
printf( "C-Style string: %s\n ", cStr);
string newStr(cStr);
newStr.erase(newStr.find( 'o ',0), 1);
strcpy(cStr, newStr.c_str());
printf( "after delete:%s\n ", cStr);
system( "pause ");
}
如char *p= "good ",怎么样把一个o给删了,如果是string str( "good ")怎么做
------解决方案--------------------
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str( "good ");
cout < < "string: " < <str < <endl;
str.erase(str.find( 'o ',0), 1);
cout < < "after delete: " < <str < <endl;
char cStr[] = "good ";
printf( "C-Style string: %s\n ", cStr);
string newStr(cStr);
newStr.erase(newStr.find( 'o ',0), 1);
strcpy(cStr, newStr.c_str());
printf( "after delete:%s\n ", cStr);
system( "pause ");
}