### Finding Words or Substrings within Strings

A wide range of functions are available to let you find words, substrings, patterns, or individual characters within a string. You can even do wildcard searches: for example, you can search through a string to find a substring that begins with the letters "Ar" followed by one or more characters, followed by the letters "rk".

The following example shows a number of the searching functions available for IString objects. Comments describe the type of search operation being carried out.

    // Searching for substrings

    #include <iostream.h>
    #include <istring.hpp>

    void main() {
       IString Str1="This string contains some sample text in English.";
       IString Str2=Str1.subString(27); // positions 27 and following:
                                        // "sample text in English."
       cout << "The string under consideration is:\n\n"
            << Str1 << "\n\n";

    // 1. Count the number of occurrences of a substring within the
    //    string
       cout << "The substring \"in\" occurs "
            << Str1.occurrencesOf("in")
            << " times in the string.\n";

    // 2. Find the first occurrence of a substring:
    //    (Note that the substring can be a char, char*, or
    //    IString value)
       cout << "The letter 'x' first occurs at position "
            << Str1.indexOf('x') << ".\n";

    // 3. Find the first occurrence of any letter of those specified:
       cout << "One of the letters q, r, or s first appears at position "
            << Str1.indexOfAnyOf("qrs") << ".\n";

    // 4. Find the first occurrence of any letter other than
    //    those specified:
        cout << "The first letter that is not in \"Think\" "
             << "appears at position "
             << Str1.indexOfAnyBut("Think") << ".\n";

    // 5. Find the index of a word
       cout << "The third word starts at position "
            << Str1.indexOfWord(3) << ".\n";

    // 6. Find a match to a phrase, and return the position of the
    //    first matching word
       cout << "The phrase \"" << Str2 << "\" starts at word number "
            << Str1.wordIndexOfPhrase(Str2) << " of the string.\n";

    // 7. Do a wildcard search to see if the string starts with "Th",
    //    contains "co", and ends with "sh."
       cout << "Does the string match the wildcard search string "
            << "\"Th*co*sh.\"?\n";
       if (Str1.isLike("Th*co*sh.")) cout << "Yes.";
       else cout << "No.";

       cout << endl;
       }

This program produces the following output:

    The string under consideration is:

    This string contains some sample text in English.

    The substring "in" occurs 3 times in the string.
    The letter 'x' first occurs at position 36.
    One of the letters q, r, or s first appears at position 4.
    The first letter that is not in "Think" appears at position 4.
    The third word starts at position 13.
    The phrase "sample text in English." starts at word number 5 of the string.
    Does the string match the wildcard search string "Th*co*sh."?
    Yes.