Next Previous Contents

6. String.h file

C++ and Java are often used concurrently in many software projects. Programmers who jump back and forth between C++ and Java will find this string class very helpful.

In C++ (or any object oriented language), you just read the "class data-structure" (i.e. interface) to begin using that class. You just need to understand the interface and not the implementation of the interface. In case of String class, you just need to read and understand the String class in String.h file. You do not need to read the entire implementation (String.cpp) in order to use String class. The object oriented classes are real time saver and they very neatly hide the implementation.

(In object oriented Java language there is the equivalent called 'interface' , which hides the implementation details.)

Given below is the sample String.h file and see also Appendix A String.h

The String.h has more than 200 string manipulation functions but below only few functions are shown as sample.


// I compiled and tested this string class on Linux (Redhat 7.1) and 
// MS Windows Borland C++ version 5.2 (win32). This should also work
// using MS Visual C++ compiler
class String
{
        public:
                String();
                virtual ~String();

                // Functions below imitate Java language's String object 
                unsigned long length();
                char charAt(int where);
                void getChars(int sourceStart, int sourceEnd, 
                                char target[], int targetStart);
                char* toCharArray();
                char* getBytes();

                bool equals(String str2);
                bool equals(char *str2);
                bool equalsIgnoreCase(String str2);

                bool regionMatches(int startIndex, String str2, 
                                int str2StartIndex, int numChars);
                bool regionMatches(bool ignoreCase, int startIndex, 
                                String str2, int str2StartIndex, int numChars);

                String toUpperCase();
                String toLowerCase();

                bool startsWith(String str2);
                bool startsWith(char *str2);

                bool endsWith(String str2);
                bool endsWith(char *str2);

                int compareTo(String str2);
                int compareTo(char *str2);
                int compareToIgnoreCase(String str2);
                int compareToIgnoreCase(char *str2);

                int indexOf(char ch, int startIndex = 0);
                int indexOf(char *str2, int startIndex = 0);
                int indexOf(String str2, int startIndex = 0);

                int lastIndexOf(char ch, int startIndex = 0);
                int lastIndexOf(char *str2, int startIndex = 0);
                int lastIndexOf(String str2, int startIndex = 0);

                String substring(int startIndex, int endIndex = 0);
                String replace(char original, char replacement);
                String replace(char *original, char *replacement);

                String trim();

                String concat(String str2);
                String concat(char *str2);
                String concat(int bb);
                String concat(unsigned long bb);
                String concat(float bb);
                String concat(double bb);

                String reverse();
                String deleteCharAt(int loc);
                String deleteStr(int startIndex, int endIndex);

                String valueOf(char ch)
                        {char aa[2]; aa[0]=ch; aa[1]=0; return String(aa);}
                String valueOf(char chars[]){ return String(chars);}
                String valueOf(char chars[], int startIndex, int numChars);
                String valueOf(bool tf)
                        {if (tf) return String("true"); else return String("false");}
                String valueOf(int num){ return String(num);}
                String valueOf(long num){ return String(num);}
                String valueOf(float num) {return String(num);}
                String valueOf(double num) {return String(num);}

                // See also StringBuffer class in this file given below

                // ---- End of Java like String object functions -----

                //////////////////////////////////////////////////////
                //              List of additional functions not in Java
                //////////////////////////////////////////////////////

                String ltrim();
                String rtrim();

                ///////////////////////////////////////////////////////////////////////
                // More than 200 string manipulation functions are provided (see the 
                // "Download String" section) but only few functions are shown here.
                ///////////////////////////////////////////////////////////////////////

                void ensureCapacity(int capacity);
                void setLength(int len);
                void setCharAt(int where, char ch);

                int parseInt(String ss) {return ss.toInteger();}
                int parseInt(char *ss)
                        {String tmpstr(ss); return tmpstr.toInteger();}
                long parseLong(String ss) {return ss.parseLong();}
                long parseLong(char *ss)
                        {String tmpstr(ss); return tmpstr.parseLong();}
                float floatValue() {return (float) toDouble(); }
                double doubleValue() {return toDouble(); }

                // All Operators ...
                String operator+ (const String  rhs);
                String operator+= (const String  rhs);
                String operator= (const String  rhs);
                bool operator== (const String  rhs);
                bool operator!= (const String  rhs);
                char operator [] (unsigned long Index) const;
                ostream operator << (ostream  Out, const String  str2);
                istream operator >> (istream  In, String  str2);
                bool String::operator< (const char rhs);

                ///////////////////////////////////////////////////////////////////////
                // More than 200 string manipulation functions are provided (see the 
                // "Download String" section) but only few functions are shown here.
                ///////////////////////////////////////////////////////////////////////
};

6.1 StringBuffer.h

C++ and Java are often used concurrently in many software projects. Programmers jump back and forth between C++ and Java will find this stringbuffer class very helpful.


//
// Author : Al Dev  Email: alavoor[AT]yahoo.com
//

// Imitate Java's StringBuffer object
// This class is provided so that the Java code is
// portable to C++, requiring minimum code changes
// Note: While coding in C++ DO NOT use this class StringBuffer,
// this is provided only for compiling code written in Java
// which is cut/pasted inside C++ code.
class StringBuffer: public String
{
        public:
                StringBuffer();
                ~StringBuffer();
                StringBuffer(char *aa);
                StringBuffer(int size);
                StringBuffer(String str);

                int capacity();
                StringBuffer append(String str2);
                        // See also operator +
                        //{ *this += str2; return *this;} // This is causing core dumps...

                StringBuffer append(char *str2);
                StringBuffer append(int bb);
                StringBuffer append(unsigned long bb) ;
                StringBuffer append(float bb) ;
                StringBuffer append(double bb) ;

                StringBuffer insert(int index, String str2);
                StringBuffer insert(int index, char ch);

                StringBuffer reverse();

                // Java's "delete()". Cannot use name delete in C++
                StringBuffer deleteStr(int startIndex, int endIndex);
                StringBuffer deleteCharAt(int loc);

                StringBuffer substring(int startIndex, int endIndex = 0);
                void assign(char *str);
};

6.2 StringTokenizer.h

C++ and Java is often used concurrently in many software projects. Programmers jump back and forth between C++ and Java will find this stringtokenizer class very helpful.


//
// Author : Al Dev  Email: alavoor[AT]yahoo.com
//

// Imitate Java's StringTokenizer class
// provided to compile Java code in C++ and vice-versa
class StringTokenizer: public String
{
        public:
                StringTokenizer(String str);
                StringTokenizer(String str, String delimiters);
                StringTokenizer(String str, String delimiters, bool delimAsToken);
                ~StringTokenizer();

                int     countTokens();
                bool    hasMoreElements();
                bool    hasMoreTokens();
                String  nextElement(); // in Java returns type 'Object'
                String  nextToken();
                String  nextToken(String delimiters);
};


Next Previous Contents