19.8. Replacing Characters in a string

Figure 19.7 demonstrates string member functions for replacing and erasing characters. Lines 10–14 declare and initialize string string1. Line 20 uses string member function erase to erase everything from (and including) the character in position 62 to the end of string1. [Note: Each newline character occupies one character in the string.]


 1   // Fig. 19.7: Fig19_07.cpp
 2   // Demonstrating string member functions erase and replace.
 3   #include <iostream>
 4   #include <string>
 5   using namespace std;
 6
 7   int main()
 8   {
 9      // compiler concatenates all parts into one string
10      string string1( "The values in any left subtree"
11         " are less than the value in the"
12         " parent node and the values in"
13         " any right subtree are greater"
14         " than the value in the parent node" );
15
16      cout << "Original string: " << string1 << endl << endl;
17
18      // remove all characters from (and including) location 62
19      // through the end of string1                            
20      string1.erase( 62 );                                     
21
22      // output new string
23      cout << "Original string after erase: " << string1
24         << " After first replacement: ";
25
26      size_t position = string1.find( " " ); // find first space
27
28      // replace all spaces with period
29      while ( position != string::npos )
30      {
31         string1.replace( position, 1, "." );
32         position = string1.find( " ", position + 1 );
33      } // end while
34
35      cout << string1 << " After second replacement: ";
36
37      position = string1.find( "." ); // find first period
38
39      // replace all periods with two semicolons
40      // NOTE: this will overwrite characters
41      while ( position != string::npos )
42      {
43         string1.replace( position, 2, "xxxxx;;yyy", 5, 2 );
44         position = string1.find( ".", position + 1 );
45      } // end while
46
47      cout << string1 << endl;
48   } // end main


Original string:
The values in any left subtree
are less than the value in the
parent node and the values in
any right subtree are greater
than the value in the parent node

Original string after erase:
The values in any left subtree
are less than the value in the

After first replacement:
The.values.in.any.left.subtree
are.less.than.the.value.in.the

After second replacement:
The;;alues;;n;;ny;;eft;;ubtree
are;;ess;;han;;he;;alue;;n;;he


Fig. 19.7. Demonstrating string member functions erase and replace.

Lines 26–33 use find to locate each occurrence of the space character. Each space is then replaced with a period by a call to string member function replace. Function replace takes three arguments: the subscript of the character in the string at which replacement should begin, the number of characters to replace and the replacement string. Member function find returns string::npos when the search character is not found. In line 32, 1 is added to position to continue searching at the location of the next character.

Lines 37–45 use function find to find every period and another overloaded function replace to replace every period and its following character with two semicolons. The arguments passed to this version of replace are the subscript of the element where the replace operation begins, the number of characters to replace, a replacement character string from which a substring is selected to use as replacement characters, the element in the character string where the replacement substring begins and the number of characters in the replacement character string to use.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset