C++ Tutorial/string/string erase — различия между версиями
Admin (обсуждение | вклад) м (1 версия: Импорт контента...) |
|
(нет различий)
|
Версия 14:21, 25 мая 2010
Содержание
Remove a word with find() and erase
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
string strA("This is a test.");
// Create an iterator to a string.
string::iterator itr;
cout << "Remove " larger".\n";
itr = find(strA.begin(), strA.end(), "l");
strA.erase(itr, itr+7);
cout << strA << "\n\n";
return 0;
}
string.erase(6,9)
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1("String handling C++ style.");
string str2("STL Power");
cout << "Initial strings:\n";
cout << "str1: " << str1 << endl;
cout << "str2: " << str2 << "\n\n";
// demonstrate erase()
cout << "Remove 9 characters from str1:\n";
str1.erase(6, 9);
cout << str1 <<"\n\n";
return 0;
}
Initial strings: str1: String handling C++ style. str2: STL Power Remove 9 characters from str1: String C++ style.
Three forms of erase() function from a string
#include <iostream>
#include <string>
using namespace std;
int main()
{
string word1 = "Game";
string word2("Over");
string word3(3, "!");
string phrase = word1 + " " + word2 + word3;
cout << phrase << endl;
phrase.erase(4, 5);
cout << "The phrase is now: " << phrase << endl;
phrase.erase(4);
cout << "The phrase is now: " << phrase << endl;
phrase.erase();
cout << "The phrase is now: " << phrase << endl;
return 0;
}
Use erase to remove all characters from (and including) location 6 through the end of string1
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
int main()
{
string string1( "abcedfghijklmnopqrstuvwxyz" );
cout << "Original string:\n" << string1 << endl << endl;
string1.erase( 6 );
cout << "Original string after erase:\n" << string1
<< "\n\nAfter first replacement:\n";
return 0;
}
Original string: abcedfghijklmnopqrstuvwxyz Original string after erase: abcedf After first replacement: