Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
Essential Tools Module User's Guide

4.3 An Introductory Example

The following example uses several essential features of the string classes. It shows the steps RWCString would take to substitute a new version number for the old ones in a piece of documentation.

Program Input:

Program Output:

The code here describes the activity of the class. RWCString creates a string object a, reads lines from standard input into a, and searches a for a pattern matching the defined regular expression "V[0-9]\\.[0-9]+". A match would be a version number between V0 and V9; for example, V1.2 and V1.22, but not V12.3. When a match is found, it is replaced with the string "V4.0".

The replacement is performed by RWTRegex<T>::replace(). The string a is passed to the function as a reference argument. After the call to replace, a is updated, replacing all occurrences of the pattern in the regular expression re with the text V4.0. The third argument of replace() is the number of occurrences of the pattern to be replaced. The 0 value indicates that all occurrences should be replaced.

You will notice that you need two backlashes in "V[0-9]\\.[0-9]+" to indicate that the special character "." is to be read literally as a decimal point. That's because the compiler removes one backslash when it evaluates a literal string. The remaining backslash alerts the regular expression evaluator to read whatever character follows literally.

In the next example, RWCString uses an overloaded operator to concatenate the strings s1 and s2. The toUpper member function converts the strings from lower to upper case, and the results are sent to cout:

See the Essential Tools Module Reference Guide for details on the string classes.

Previous fileTop of DocumentContentsIndex pageNext file

©2004 Copyright Quovadx, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Quovadx, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.