Copying Strings

You can copy IStrings using copy constructors, assignment operators, and substring functions.

The IString assignment operator and copy constructor both copy one string to another string. One of the strings can be an array of characters, or both may be IString objects. The IString assignment operator and copy constructor offer the following advantages over the strcpy and strdup functions provided by the C string.h library:

Creating Substrings of Strings

You can use the subString function to return a new IString object containing a portion of another IString. This function lets you create an IString containing the leftmost characters, rightmost characters, or characters in the string's middle. The following example shows calls to subString that create substrings with leftmost, rightmost, or middle characters:

// Using the subString method of IString
#include <iostream.h>
#include <istring.hpp>
void main() {
   IString All("This is the entire string.");
   // Left -> subString(1, length)
   IString Left=All.subString(1,5); 
   // Middle -> (startpos, length)
   IString Middle=All.subString(6,14); 
   // Right -> (string length - (substring length - 1) )
   IString Right=All.subString(All.length()-6); 
   cout << "<" << All << ">\n"
        << "<" << Left << ">\n"
        << "<" << Middle << ">\n"
        << "<" << Right << ">" << endl;
   } 

This program produces the following output:

<This is the entire string.>
<This >
<is the entire >
<string.>