Testing the Characteristics of Strings

The IString class lets you test your strings to determine characteristics such as the following:

This list covers only a few of the testing functions provided by IString.

The testing functions return a value of type Boolean or IBoolean, indicating either True or False for the tested characteristic. For example, the function isBinaryDigits() returns false for the IString value "1101121101". All testing functions return a value of false for null IString.

The testing functions all have names beginning with is..., because they ask a question, such as "is the IString made up only of binary digits?" The following example shows how you can use a subset of these functions:

// Evaluating strings using the IString is... methods
#include <istring.hpp>
#include <iostream.h>
void evaluate(IString& StringToTest) {
   if (StringToTest.isPrintable())
      cout << "Evaluating the string " << StringToTest << ":" << endl;
   else
      cout << "Evaluating an unprintable string:" << endl; 
   if (StringToTest.isDigits())
      cout << " Contains only digits 0-9." << endl; 
   if (StringToTest.isAlphabetic())
      cout << " Contains only alphabetic characters." << endl; 
   if (StringToTest.isAlphanumeric())
      cout << " Contains only alphabetic and numeric characters." << endl; 
   if (StringToTest.isBinaryDigits())
      cout << " Contains only zeros and ones." << endl; 
   if (StringToTest.isHexDigits())
      cout << " Contains only hex digits 0-9, a-f, A-F." << endl; 
   if (StringToTest.isControl())
      cout << " Contains only ASCII values 00-1F, 7F." << endl; 
   if (StringToTest.isLowerCase())
      cout << " Contains only lowercase letters a-z." << endl; 
   if (StringToTest.isUpperCase())
      cout << " Contains only uppercase letters a-z." << endl; 
   if (StringToTest.isSBCS())
      cout << " Contains only SBCS characters." << endl;
   }
void main() {
   IString Str[6];
   Str[0]="12345";                    // numeric, hexadecimal
   Str[1]="abcde";                    // alphabetic, hexadecimal
   Str[2]="10101";                    // numeric, binary
   Str[3]="abCde";                    // alphabetic, hexadecimal 
   Str[4]="xyz12";                    // alphanumeric, lowercase
   Str[5]="\x04\x06\x11\x12";         // control, unprintable
   for (int i=1;i<6;i++) evaluate(Str[i]);
   } 

The output from this program resembles the following. Depending on the code page and character set (ASCII or EBCDIC) of the system you are running the program on, the results may vary.

Evaluating the string abcde:
   Contains only alphabetic characters.
   Contains only alphabetic and numeric characters.
   Contains only hex digits 0-9, a-f, A-F.
   Contains only lowercase letters a-z.
   Contains only SBCS characters.
Evaluating the string 10101:
   Contains only digits 0-9.
   Contains only alphabetic and numeric characters.
   Contains only zeros and ones.
   Contains only hex digits 0-9, a-f, A-F.
   Contains only SBCS characters. 
Evaluating the string abCde:
   Contains only alphabetic characters.
   Contains only alphabetic and numeric characters.
   Contains only hex digits 0-9, a-f, A-F.
   Contains only SBCS characters.
Evaluating the string xyz12:
   Contains only alphabetic and numeric characters.
   Contains only SBCS characters.
Evaluating an unprintable string:
   Contains only ASCII values 00-1F, 7F.
   Contains only SBCS characters.