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
 
 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
 

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.

Introduction to Computers, the Internet and World Wide Web

Introduction to C++ Programming

Introduction to Classes and Objects

Control Statements: Part 1

Control Statements: Part 2

Functions and an Introduction to Recursion

Arrays and Vectors

Pointers and Pointer-Based Strings

Classes: A Deeper Look, Part 1

Classes: A Deeper Look, Part 2

Operator Overloading; String and Array Objects

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

Templates

Stream Input/Output

Exception Handling

File Processing

Class string and String Stream Processing

Web Programming

Searching and Sorting

Data Structures

Bits, Characters, C-Strings and structs

Standard Template Library (STL)

Other Topics

Appendix A. Operator Precedence and Associativity Chart

Appendix B. ASCII Character Set

Appendix C. Fundamental Types

Appendix D. Number Systems

Appendix E. C Legacy Code Topics

Appendix F. Preprocessor

Appendix G. ATM Case Study Code

Appendix H. UML 2: Additional Diagram Types

Appendix I. C++ Internet and Web Resources

Appendix J. Introduction to XHTML

Appendix K. XHTML Special Characters

Appendix L. Using the Visual Studio .NET Debugger

Appendix M. Using the GNU C++ Debugger

Bibliography



C++ How to Program
C++ How to Program (5th Edition)
ISBN: 0131857576
EAN: 2147483647
Year: 2004
Pages: 627

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net