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

- When an IString object is copied, a new copy of the string is not made. Instead, the two strings point to the same buffer location. The object is only copied if one of the strings is changed. This means that, for strings that are copied but where neither the source string nor the copy is subsequently changed, performance is improved by the amount of time it would have taken to make the new copy.
- The notation is simple and intuitive. To copy String1 into String2, you simply code String2=String1. With strings defined using the traditional char* method, such an assignment merely copies a pointer to the original string. With IString objects, the assignment copies each byte of the string into the new string.
- You do not have to determine the length of the source string and allocate sufficient storage to store it in the target string before the assignment. IString takes care of allocating the storage for you, whether the target string is being constructed within the assignment or has already been constructed. This reduces the risk of memory violations. In the following example, String2 is constructed and initialized, and then copied to (its original contents are overwritten), while String3 is copy-constructed to contain a copy of String1. Notice that String2's length is extended by the assignment operation.

      IString String1="A longer string than String2";
      IString String2="A short string";
      IString String3=String1;    // initialized to String1
      String2=String1;            // extended to fit String1

- The string being copied can contain null characters anywhere within it, and the entire string will be copied.
- If you accidentally create an array of characters without the terminating null, the strcpy function may continue copying past the storage allocated for the string. This can cause storage violations, or, at the least, it can corrupt the data in the target string. The length of IString objects is not determined by a terminating null, so storage violations and corrupt target strings are less likely.

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