The IString class is the most basic of the utility classes. Items of type IString contain and manipulate character data. Data types other than characters can be stored in a IString object, however, other types of data will first be converted to a character representation. IString objects automatically grow and shrink as their content is manipulated and are not pre-allocated to a known size.
The complete information on the functionality of the IString class is found in the ISTRING.HPP header file. This class and associated files were taken from what became part of a user interface class library in the IBM C++ product. The following is a subset of the constructor methods available to create IString objects:
IString(); IString( const char* ); IString( int ); IString( const IString& ); IString( char ); IString( const void *pBuffer1, unsigned lenBuffer1, char padCharacter=' ' );
The following is a subset of the methods available for IString objects:
long asInt() const; unsigned size() const; unsigned length() const; IString subString( unsigned startPos, unsigned length=0, char padCharacter=' ' ) const;
The following is a subset of the overloaded operator methods available for IString objects:
char &operator[]( unsigned index ); IString &operator=( const IString &aString ); IString operator + ( const IString &aString ) const; IString operator + ( const char *pString ) const; friend _export IString operator + ( const char *pString, const IString &aString ); IString operator - ( const IString &aString ) const; IString operator - ( const char *pString ) const; friend _export IString operator - ( const char *pString, const IString &aString ); IString & operator += ( const IString &aString ); IString & operator += ( const char *pString ); operator char*() const; const char &operator[]( unsigned index ) const;\
The == operator prototyped below works similarly for operators: !=, <, <=, >, and >=.
friend _export Boolean operator == ( const IString &string1, const IString &string2 ); friend _export Boolean operator == ( const IString &string1, const char *pString2 ); friend _export Boolean operator == ( const char *pString1, const IString &string2 );
IString objects can be automatically instantiated on the stack as shown below. This example creates a IString object named str:
#include "istring.hpp" . . . IString str;
IString objects can also be dynamically instantiated by way of a constructor method. Below is an example of constructing a IString and referencing it with a pointer named pstr:
#include "istring.hpp" . . IString * pstr; . . pstr = new IString("ABC");
IStrings act as a repository for data given to them. All data given to an IString object is first converted to ASCII character data and then copied into the IString. Thus, after constructing an IString, data may be deposited into it such as the following:
str = "Output from YourFunction:\n";The creating and initialization of a IString object may be combined as the following:
IString str("Output from YourFunction:\n");Used as above in the first example, with the = operator, any existing content of the IString item is entirely replaced with the new data, Output from YourFunction.
The IString class provides additional overridden operators for manipulating the content of an IString object. To concatenate data to an existing IString item, use the +=operator:
str += (IString)"Input ABC returned XZY\n";
or,
str = (IString)"Input ABC " + (IString)"returned XYZ\n";
The following is also an eraser or subtraction operator which deletes the first occurrence of the specified substring.
(str = str - (IString)"returned")If the above three IString operations were performed in sequence, str would contain the following:
Output from YourFunction:\n Input ABC XZY\n
Notice the use of the (IString) casting operator on the RHS operands when using the + operator. This function is necessary for the + and - overridden IString operators in order to keep the compiler from getting confused and trying to do the wrong type of "addition" or "subtraction".
In addition to character strings, IString items can also accept other data types. IString items recognize these other data types and convert the data into a character string.
The user must tell the IString what type of data is being input through the use of casting operators, for example:
str += (int)101;The integer will be converted to its base 10, character equivalent and the resultant content of str would be the:
Output from YourFunction:\n Input ABC XZY\n 101
IString items can accept for input and convert the following data types:
(const char *) // Deref's ptr, copies in string, %s (char) // Converts to ASCII, %c (IString &) // Deref's ref, copies in IString (int) // Converts integer to ASCII, %d (void *) // Converts pointer to integer ASCII, %p
One last overridden operator is the square brackets, [], which provide similar function as they do for normal C strings except the index origin begins with 1. They provide a convenient mechanism for indexed access to individual characters within the IString. The index has an origin of 1. From the above example, str[2] is currently equal to the character u.
IString substrings can be accessed through the following subString method:
str = (IString)"ABCEFG"; IString str2 = str.subString(3,3);
After executing these lines, str2 contains CEF. Although IString objects store all data internally as character data, several methods are provided to retrieve data types other than characters. For example:
int i; IString str; . . . i = 5; str = (IString)2 + (IString)i; i = str.asInt(); // Returns IString as an integer
After this code executes, str[1] = '2', str[2] = '5', and i = 25.
A length method is included that returns the current number of characters stored in the following IString object:
i = str.length();
results in:
i = 2.