Demonstrate insert(), erase(), and replace(). : String « Data Type « C++

Home
C++
1.Bitset
2.Class
3.Console
4.Data Structure
5.Data Type
6.Deque
7.Development
8.File
9.Function
10.Generic
11.Language
12.List
13.Map Multimap
14.Overload
15.Pointer
16.Qt
17.Queue Stack
18.Set Multiset
19.STL Algorithms Binary search
20.STL Algorithms Heap
21.STL Algorithms Helper
22.STL Algorithms Iterator
23.STL Algorithms Merge
24.STL Algorithms Min Max
25.STL Algorithms Modifying sequence operations
26.STL Algorithms Non modifying sequence operations
27.STL Algorithms Sorting
28.STL Basics
29.String
30.Valarray
31.Vector
C / ANSI-C
C Tutorial
C++ Tutorial
Visual C++ .NET
C++ » Data Type » StringScreenshots 
Demonstrate insert(), erase(), and replace().
Demonstrate insert(), erase(), and replace().
 

#include <iostream>
#include <string>
using namespace std;

int main()
{
  string str1("www.java2java.com");
  string str2("STL Power");

  cout << "Initial strings:\n";
  cout << "str1: " << str1 << endl;
  cout << "str2: " << str2 << "\n\n";

  // insert()
  cout << "Insert str2 into str1:\n";
  str1.insert(6, str2);
  cout << str1 << "\n\n";

  // erase()
  cout << "Remove 9 characters from str1:\n";
  str1.erase(69);
  cout << str1 <<"\n\n";

  // replace 
  cout << "Replace 8 characters in str1 with str2:\n";
  str1.replace(78, str2);
  cout << str1 << endl;

  return 0;
}


           
         
  
Related examples in the same category
1.String type classString type class
2.A filter to remove white-space characters at the ends of lines.A filter to remove white-space characters at the ends of lines.
3.A string demonstration: assignment, concatenate, compareA string demonstration: assignment, concatenate, compare
4.Use string: find, string::nposUse string: find, string::npos
5.Strings: size, iterator, count, begin and endStrings: size, iterator, count, begin and end
6.string: find( ) and rfind( )string: find( ) and rfind( )
7.Print a name in two different formatsPrint a name in two different formats
8.Several string operations: substrSeveral string operations: substr
9.String Char IndexingString Char Indexing
10.String Find and replaceString Find and replace
11.String SizeString Size
12.String SizeOfString SizeOf
13.A short string demonstrationA short string demonstration
14.String insert(), erase(), and replace()String insert(), erase(), and replace()
15.string variable instead of a character arraystring variable instead of a character array
16.Inputting Multiple Words into a StringInputting Multiple Words into a String
17.Adding StringsAdding Strings
18.Read string from consoleRead string from console
19.Insert, search, and replace in strings.Insert, search, and replace in strings.
20.Accessing Characters In StringsAccessing Characters In Strings
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.