String
[Previous] [Main] [Next]

 
String is a stand-alone class. It manages variable-length strings, providing functions and operators similar to those in the Basic language. It is designed to simplify management of C-style null-terminated string arrays.  
 
String objects have the following features:  
  • They can grow as a result of concatenation (the + operator).
  • They can shrink as a result of subtraction (the - operator).
  • They can be freely substituted for char * function arguments.
  • They can be output using the << stream I/O operator.
 
Constructors and operators are also provided for creating, assigning, and comparing String object and char * arrays. You can think of String objects as strings, rather than simply arrays of characters. It's important to note that String objects are self-contained, having their own private copy of their data. Strings objects are copied when assigned to one another, and do not share data. This is evident in the example below, where the copied string contents do not change when the original string is modified:  
 
   String  my_string = "abcdefg" ;  
   String  copy_string = my_string ;  
   my_string.ToUpper() ;  
   //  my_string contains "ABCDEFG"  
   //  copy_string contains "abcdefg"  
 
You can assign C-style strings to a String object using the = operator:  
 
   String  my_string = "abcdefg" ;  
 
You can assign one String object to another (i.e., copy):  
 
   String  my_string = "abcdefg" ;  
   String  copy_string = my_string ;  
 
To concatenate two strings, use the + or += addition operators:  
 
   String  str1 = "abcdefg" ;  
   String  str2 = "HIJKLMNOP" ;  
   String  combined = str1 + str2 + "qrstuv" ;  
   //  combined now contains "abcdefgHIJKLMNOPqrstuv"  
 
To subtract one String from another, use the - or -= subtraction operators (subtraction is performed from left to right):  
 
   String  str = "abcdefg" ;  
   String  result = str - "def" ;  
   //  result now contains "abcg"  
 
Note: At least one of the arguments in an addition or subtraction operation must be a String object. For example, to concatenate three literals, you might use the following:  
 
   String  str = String("abc") + "DEF" + "ghi" ;  
 
String overloads the comparison operators so that Strings are compared based on their contents, rather than their addresses. Comparing String objects is easy using the following operators, all of which are case sensitive:  
 
   ==   equality  
   !=   not equal  
   >   greater than  
   >=   greater than or equal  
   <   less than  
   <=   less than or equal  
 
   String  str = "abcdefg" ;  
   if ( str == "abcdefg" )  
   {  
      //  strings are identical  
   }  
 
For easy integration with C-language string functions, you can manipulate String objects as if they were null-terminated C-style character arrays:  
  • You can use the GetString() method to create a copy of the string contents, returning a pointer to the newly allocated buffer as a char * pointer. It is the responsibility of the application to release the buffer using the C++ delete operator.
  • You can cast the object to char *, which returns a pointer to the internal C-style buffer used to store the string contents. For example:
 
   String  str = "abcdefg" ;  
   printf( "%s\n", (char *) str ) ;  
 
Individual characters within a string can be accessed using the [ ] array element operator:  
 
   String  my_string = "abcdefg" ;  
   for ( int i = 0 ; i < my_string.GetLength() ; i++ )  
      cout << my_string[i] ;  
 
Portions of a string can be extracted using the following methods:  
 
   Left( int length )  
   Mid( int start, int length )  
   Right( int length )  
 
The following examples illustrate use of the substring methods:  
 
   String  str = "abcdefg" ;  
   String  substr ;  
    substr = str.Left( 3 ) ;   // substr contains "abc"  
   substr = str.Right( 3 ) ;  // substr contains "efg"  
   substr = str.Mid( 1, 2 ) ; // substr contains "bc"  
 
String objects make working with numbers particularly easy. The assignment operator is overloaded to accept not just other Strings and char * pointers, but also int, long, and double values as well:  
 
   String  str = 67.5 ;  // str contains "67.5"  
 
The C-language sprintf() routine is often used to format strings containing various data types. For convenience, the String class contains a Format() method, which operates in a similar fashion to sprintf(), accepting a format specifier and a variable number of arguments. String's Format() method offers much more convenience, however, as it makes use of the String class's memory management features to allocate an appropriately-sized internal storage buffer.  
 
   String  str ;  
   str.Format( "%s: %02d/%02d/%04d",  
                   "The date is", 1, 1, 1998 ) ;  
   //  str contains "The date is: 01/01/1998"  


©1998 DC Micro Development. All rights reserved.
No portion of this document may be c opied or reproduced without expressed written consent.
html++ is a trademark of DC Micro Development.