home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / df3os2.zip / STRINGS.H < prev    next >
C/C++ Source or Header  |  1993-09-23  |  2KB  |  75 lines

  1. // -------- strings.h
  2.  
  3. #ifndef STRINGS_H
  4. #define STRINGS_H
  5.  
  6. #include <iostream.h>
  7. #include <string.h>
  8. #include "dflatdef.h"
  9.  
  10. // ============================
  11. // BASIC-like String Class
  12. // ============================
  13. class String    {
  14.     char *sptr;
  15.     int length;
  16.     void putstr(const char *s);
  17. public:
  18.     // -------- construct a null string
  19.     String() { sptr = 0; length = 0; }
  20.     // --- construct with char * initializer
  21.     String(const char *s);
  22.     // ------- copy constructor
  23.     String(String& s);
  24.     // -------- construct with a size and fill character
  25.     String(int len, char fill = 0);
  26.     // ------- destructor
  27.     ~String() { delete sptr; }
  28.     // ------ return the length of a string
  29.     int Strlen() { return strlen(sptr); }
  30.     int StrBufLen() { return length; }
  31.     // ------ change the buffer length of a string
  32.     void ChangeLength(unsigned int newlen);
  33.     // ---- substring: right len chars
  34.     String right(int len);
  35.     // ---- substring: left len chars
  36.     String left(int len);
  37.     // ---- substring: middle len chars starting from where
  38.     String mid(int len, int where);
  39.     int FindChar(unsigned char ch);
  40.     // ---------- assignment
  41.     String& operator=(String& s);
  42.     // ---------- conversion to char *
  43.     operator const char *() const { return sptr; }
  44.     // --- concatenation operator (str1 + str2;)
  45.     String operator+(String& s);
  46.     // --- concatenation operator (str1 += str2;)
  47.     void operator+=(String& s);
  48.     // --- concatenation operator (str1 + "str")
  49.     String operator+(const char *s);
  50.     // --- concatenation operator (str1 += "str")
  51.     void operator+=(const char *s);
  52.  
  53.     // ------- relational operators
  54.     Bool operator==(String& s)
  55.         { return (Bool) (strcmp(sptr,s.sptr) == 0); }
  56.     Bool operator!=(String& s)
  57.         { return (Bool) (strcmp(sptr,s.sptr) != 0); }
  58.     Bool operator>(String& s)
  59.         { return (Bool) (strcmp(sptr,s.sptr) > 0); }
  60.     Bool operator<(String& s)
  61.         { return (Bool) (strcmp(sptr,s.sptr) < 0); }
  62.     Bool operator<=(String& s)
  63.         { return (Bool) (!(*this > s)); }
  64.     Bool operator>=(String& s)
  65.         { return (Bool) (!(*this < s)); }
  66.     // ------- subscript
  67.     char& operator[](int n) { return sptr[n]; }
  68.     // ------- stream I/O
  69.     friend ostream& operator<< (ostream& os, String& str);
  70.     friend istream& operator>> (istream& is, String& str);
  71. };
  72.  
  73. #endif
  74.  
  75.