Replacing Characters in a string
Figure 18.7 demonstrates string member functions for replacing and erasing characters. Lines 1317 declare and initialize string string1. Line 23 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 element in the string.]
Figure 18.7. Demonstrating functions erase and replace.
(This item is displayed on pages 897 - 898 in the print version)
1 // Fig. 18.7: Fig18_07.cpp 2 // Demonstrating string member functions erase and replace. 3 #include 4 using std::cout; 5 using std::endl; 6 7 #include 8 using std::string; 9 10 int main() 11 { 12 // compiler concatenates all parts into one string 13 string string1( "The values in any left subtree" 14 " are less than the value in the" 15 " parent node and the values in" 16 " any right subtree are greater" 17 " than the value in the parent node" ); 18 19 cout << "Original string: " << string1 << endl << endl; 20 21 // remove all characters from (and including) location 62 22 // through the end of string1 23 string1.erase( 62 ); 24 25 // output new string 26 cout << "Original string after erase: " << string1 27 << " After first replacement: "; 28 29 int position = string1.find( " " ); // find first space 30 31 // replace all spaces with period 32 while ( position != string::npos ) 33 { 34 string1.replace( position, 1, "." ); 35 position = string1.find( " ", position + 1 ); 36 } // end while 37 38 cout << string1 << " After second replacement: "; 39 40 position = string1.find( "." ); // find first period 41 42 // replace all periods with two semicolons 43 // NOTE: this will overwrite characters 44 while ( position != string::npos ) 45 { 46 string1.replace( position, 2, "xxxxx;;yyy", 5, 2 ); 47 position = string1.find( ".", position + 1 ); 48 } // end while 49 50 cout << string1 << endl; 51 return 0; 52 } // end main
|
Lines 2936 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 35, 1 is added to position to continue searching at the location of the next character.
Lines 4048 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.