Next Previous Contents

5. Usage of String class

Take notice, this String class is not the same as the string class implemented in the Standard C++ Library. This special String class is a "home-made" String class, made to help Java programmers convert to C++. When you are more comfortable with C++, you should use the real string class provided in The Standard C++ Library.

To use String class, you should first refer to a sample program "example_String.cpp" given in Appendix A and the String class which is given in Appendix A.

The 'String class' is a complete replacement for char and char * datatype. You can use 'String class' just like char and get much more functionalities. You should link with the library 'libString.a' which you can build from the makefile given in Appendix A and copy the library to /usr/lib or /lib directory where all the C++ libraries are located. To use the 'libString.a' compile your programs like -


        g++ example.cpp -lString

See illustration sample code as given below -
        String aa;

        aa = "Creating an Universe is very easy, similar to creating a baby human.";

        // You can use aa.val() like a 'char *' variable in programs
        for (unsigned long tmpii = 0; tmpii < aa.length(); tmpii++)
        {
                //fprintf(stdout, "aa.val()[%ld]=%c ", tmpii, aa.val()[tmpii]);
                fprintf(stdout, "aa[%ld]=%c ", tmpii, aa[tmpii]);
        }

        // Using pointers on 'char *' val ...
        for (char *tmpcc = aa.val(); *tmpcc != 0; tmpcc++)  
        {
                fprintf(stdout, "aa.val()=%c ", *tmpcc);
        }

5.1 Operators

The 'String class' provides these operators :-

For example to use operators -
        String aa;
        String bb("Bill Clinton");

        aa = "put some value string";  // assignment operator
        aa += "add some more"; // Add to itself and assign operator
        aa = "My name is" + " Alavoor Vasudevan "; // string cat operator

        if (bb == "Bill Clinton")  // boolean equal to operator
                cout << "bb is equal to 'Bill Clinton' " << endl;

        if (bb != "Al Gore")   // boolean 'not equal' to operator
                cout << "bb is not equal to 'Al Gore'" << endl;

5.2 Functions

The functions provided by String class have the same name as that of Java language's String class. The function names and the behaviour is exactly the same as that of Java's String class. StringBuffer class is also provided. This will facilitate portability of code between Java and C++ (you can cut and paste and do minimum changes to code). The code from Java's function body can be copied into C++ member function body and with very mininum changes the code will compile under C++. Another advantage is that developers coding in both Java and C++ do not need to remember two different syntax or function names.

For example to convert integer to string do -


        String  aa;

        aa = 34;  // The '=' operator will convert int to string
        cout << "The value of aa is : " << aa.val() << endl;

        aa = 234.878;  // The '=' operator will convert float to string
        cout << "The value of aa is : " << aa.val() << endl;

        aa = 34 + 234.878;
        cout << "The value of aa is : " << aa.val() << endl;
        // The output aa will be '268.878'

        // You must cast String to convert
        aa = (String) 34 + " Can create infinite number of universes!! " + 234.878;
        cout << "The value of aa is : " << aa.val() << endl;
        // The output aa will be '34 Can create infinite number of universes!! 234.878'

Refer to Appendix A String.h for details about the String class function names. The same file String.h is reproduced here in next section.

5.3 Renaming the String class

Case 1: Simple rename

If you do not like the String class name then you can use "typedef" to rename the String class.

In all the files where you do include String.h, insert these lines:


// If you do not like the class name String, then you can rename using typedef
typedef String StringSomethingElseIwant;

// Your remaining code may be like this ....
int main()
{
        StringSomethingElseIwant aa_renstr; 
        aa_renstr = "I renamed the String Class using typedef";

        // .......etc...
}

See the example_String.cpp click on 'Source code for C++'.

Case 2: Resolve conflict

If there is a conflict with another class-name having the same name, and you want to use both this class and conflicting class then you use this technique - in all the files where you do include String.h, insert these lines:


#define  String  String_somethingelse_which_I_want
#include "String.h"
#undef String

#include "ConflictingString.h"  // This also has String class...

// All your code goes here...
int main()
{
        String_somethingelse_which_I_want aa;
        String bb; // This string class from conflicting string class

        aa = " some sample string";
        bb = " another string abraka-dabraka";
        .......
}

The pre-processor will replace all literals of String to "String_somethingelse_which_I_want" and immediately undefines String. After undef the conflicting string class header file is included which defines the "String" class.


Next Previous Contents