Simple String Class

Environment: VC6, NT4 SP5, Windows 95b/98


Although both MFC and STL provide the CString and the string class to manipulate strings, In my recent project I have to develop a simple string class for my particular use. The future includes formatting string, similar to MFC format; searching string with the given wildcards; and numeric conversion with the desired data type.

Class Definition

class CStr
 // constructors and destructor
 CStr(const CStr& str);
 CStr(const char* str);
 CStr(const double var)				
  { VarToString(var); }

  { m_nLength = 0; m_pString = 0; }

 virtual ~CStr()						
  { if (m_pString) delete m_pString; }

 // operator overloading helper
 template <class T> friend CStr _cdecl operator +(T var, const CStr& str);

 // operator overloading
 CStr& operator  =(const char* str);
 CStr& operator  =(const CStr& str);

 CStr& operator  =(const double var)	
 { VarToString(var); return *this; }

 template<class T>
 CStr  operator  +(T var)			
 { CStr tstr = *this; return tstr += var; }

 CStr& operator +=(double str)		
 { return *this += (CStr)str; }

 CStr& operator +=(const char* str)	
 { return *this += (CStr)str; }

 CStr& operator +=(const CStr& str);

 // add more logic comparison operators as following,
 // for example, although not efficient
 virtual bool operator !=(char* str)	
  { return strcmp(str, m_pString) != 0; }

 // c type string conversion
 operator char* ()
  { return m_pString; }

 operator const char* () const		
  { return m_pString; }

 char* GetChar()
  { return m_pString; }

 // numeric conversion
 template <class T> GetValue(T& var)
  { return GetVar(var); }

 // search the match string : 
 // WildCards can be '?' and '*' combination
 // return value : true (pattern matchs string), false (no match)
 bool Search(const char* WildCards)	
  { return Match((char*)WildCards, m_pString); }

 // format string
 int Format(const char* format, ...);

 // can use faster algorithm for search ?
 virtual bool Match(char*, char*);

 virtual bool Scan(char*&, char*&);

 // have any good conversion method ?
 virtual void VarToString(const double var);

 // numeric conversion helpers
 bool NumericParse(void* pvar, char flag);
 bool GetVar(bool& var)				
  { return NumericParse((void*)&var, 'b'); }

 bool GetVar(char& var)				
  { return NumericParse((void*)&var, 'c'); }

 bool GetVar(short& var)				
  { return NumericParse((void*)&var, 's'); }

 bool GetVar(int& var)				
  { return NumericParse((void*)&var, 'i'); }

 bool GetVar(long& var)				
  { return NumericParse((void*)&var, 'l'); }

 bool GetVar(float& var)				
  { return NumericParse((void*)&var, 'f'); }

 bool GetVar(double& var)			
  { return NumericParse((void*)&var, 'd'); }

 bool GetVar(unsigned char& var)		
  { return NumericParse((void*)&var, 'C'); }

 bool GetVar(unsigned short& var)	
  { return NumericParse((void*)&var, 'S'); }

 bool GetVar(unsigned int& var)		
  { return NumericParse((void*)&var, 'I'); }

 bool GetVar(unsigned long& var)		
  { return NumericParse((void*)&var, 'L'); }

// data block
 int   m_nLength;
 char* m_pString;

template <class T>
CStr operator +(T var, const CStr& str) 
 CStr svar = var;
 return svar += str; 

Source Code

The implemented file Str.cpp is independent on MFC and STL string class. Hope this simple string class can be used in most platform. Note that some UNIX C++ compilers do not support snprintf.

Demo Project

StrDemo.cpp demonstrates how to use string numeric conversion, format, and search. Note that the numeric type plus CStr is legal, such as 6.456 + (CStr)"hyt" will produce "6.456hyt" and (CStr)"hyt" + '1' will produce "hyt49". If you do not like that, please modify binary operator CStr operator +(T var, const CStr& str) and corresponding member functions.


Download demo project - 6 Kb
Download source - 3 Kb


  • self assignment

    Posted by jambodev on 09/12/2010 05:04am

    This is very useful, but has a problem of self assignment, it doesn't check for self assignment:
    if we doe:
    this will crash.

  • You forgot something important: UNICODE.

    Posted by Legacy on 11/24/2002 12:00am

    Originally posted by: haiku

    Of course I know that this is free stuff, but you forgot something really important.

  • Help

    Posted by Legacy on 08/22/2002 12:00am

    Originally posted by: Gump

    Can you briefly describe what is the difference between MFC and STL? thanks!!!


    Posted by Legacy on 04/06/2002 12:00am

    Originally posted by: Waseema Rehman

    i like the work. please keep up the good work. hope that it will be helpful to others as it helped me.


    Posted by Legacy on 01/10/2002 12:00am

    Originally posted by: Daud Nasir

    this is nice work and help me a lot. thanks for putting it on the web.

  • No GetLength()?

    Posted by Legacy on 05/07/2001 12:00am

    Originally posted by: Christian Cheney

    I know it's easy to add, but any reason that you didn't include GetLength( )?

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Wednesday, September 24, 2014 8:00 AM - 9:00 AM PDT According to a recent Forrester Research report, many companies are choosing low-code platforms over traditional programming platforms, due to the speed with which low-code apps can be assembled and tested. With customer-facing applications on the rise, traditional programming platforms simply can't keep up with the "short schedules and rapid change cycles" required to develop these applications. Check out this upcoming webinar and join Clay Richardson from …

  • As mobile devices have pushed their way into the enterprise, they have brought cloud apps along with them. This app explosion means account passwords are multiplying, which exposes corporate data and leads to help desk calls from frustrated users. This paper will discover how IT can improve user productivity, gain visibility and control over SaaS and mobile apps, and stop password sprawl. Download this white paper to learn: How you can leverage your existing AD to manage app access. Key capabilities to …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds