Visual Studio .NET 2002Visual C++ 7.1Visual Studio 6Visual C++ 7.0Visual Studio .NET 2003Windows 2003Windows 2000Visual C++ 6.0Windows XPIntermediateDevVisual StudioWindowsC++
KString class, a non MFC string class






3.01/5 (33 votes)
KString class, a non MFC string class.
Introduction
We needed a customized and non MFC String class, so I wrote the KString
class. Any suggestions and refinements are welcome.
Here are the KString
methods (Visual C++ 2003):
class KString { public: // - Constructions: KString(void); // - Default constructor KString(const LPCTSTR lpszStr); // - Constructor KString(const KString &sStr); // - Copy constructor // - Destruction ~KString(void); // - Destructor // - Operators Overloading // - Operator = overloading for KString KString& operator =(const KString &sStr); // - Operator = overloading for string KString& operator =(LPCTSTR lpszStr); // - Operator = overloading for int KString& operator =(const int iNum); // - Operator = overloading for double KString& operator =(const double fNum); // - Operator + overloading : KString + KString KString operator +(const KString &sStr); // - Operator + overloading : KString + LPCTSTR KString operator +(LPCTSTR lpszStr); // - Operator + overloading : KString + int KString operator +(int iNum); // - Operator + overloading : KString + double KString operator +(double fNum); // - Operator +=overloading for KString KString& operator+=(const KString &sStr); // - Operator +=overloading for LPCTSTR KString& operator+=(LPCTSTR lpszStr); // - Operations // - Operator < overloading BOOL operator <(const KString &sStr); // - Operator <= overloading BOOL operator <=(const KString &sStr); // - Operator > overloading BOOL operator >(const KString &sStr); // - Operator >= overloading BOOL operator >=(const KString &sStr); // - Operator ==overloading for KString BOOL operator==(const KString &sStr); // - Operator ==overloading for LPCTSTR BOOL operator==(LPCTSTR sStr); // - Operator !=overloading for KString BOOL operator!=(const KString &sStr); // - Operator !=overloading for LPCTSTR BOOL operator!=(LPCTSTR sStr); // - Operator prefix -- overloading KString& operator--(); // - Operator postfix -- overloading KString operator--(int); // - LPTSTR type cast operator LPTSTR(){return m_sString;} // - Returns TCHAR in specified position TCHAR GetAt(int iPos); // - Finds first position of specified TCHAR int Find(TCHAR ch); // - Finds first position of specified KString int Find(const KString &sStr); // - Finds first position of specified string int Find(LPCTSTR lpszStr); // - Replace an old TCHAR with a new TCHAR int Replace(TCHAR cOld, TCHAR cNew); // - Replace an old string with a new string int Replace(LPCTSTR sOld, LPCTSTR sNew); // - Replace an old KString with a new KString int Replace(const KString &sOld, const KString &sNew); // - Returns iCount TCHARs from iStart KString Mid(int iStart, int iCount); // - Returns TCHARs from nStart till the end KString Mid(int iStart); // - Returns iCount left TCHARs KString Left(int iCount); // - Returns iCount right TCHARs KString Right(int iCount); // - Removes a TCHAR in specified position BOOL Remove(int iPos); // - Removes TCHARs from nStart to nEnd int Remove(int iStart, int iEnd); // - Removes all ch TCHAR int Remove(TCHAR ch); // - Removes all specified string BOOL Remove(const KString &sStr); // - Trims left specified TCHAR KString& TrimLeft(TCHAR ch = ' '); // - Trims right specified TCHAR KString& TrimRight(TCHAR ch = ' '); // - Trims left and right specified TCHAR KString& Trim(TCHAR ch = ' '); // - Inserts a string in specified position KString& Insert(const KString &sStr, int iPos); // - Appends a string KString& Append(const KString &sStr){return *this += sStr;}; KString& MakeUpper(); // - Converts to upper case KString& MakeLower(); // - Converts to lower case KString& MakeReverse(); // - Reverses the string KString GetUpper(); // - Returns uppercase of string KString GetLower(); // - Returns lowercase of string KString GetReverse(); // - Returns reverse of string void Empty(); // - Clears the string // - Return length of the string int GetLength(){return m_iLength;} // - Return size of the string int GetSize(){return m_iLength*sizeof(TCHAR);} // - return TRUE if the string is empty BOOL IsEmpty(){return m_iLength ? FALSE : TRUE;} // - Creates a sized buffer LPTSTR GetBuffer(int iBuffSize); // - Frees extra space void FreeExtra(); // - Format string void Format(LPCTSTR pszCharSet, ...); // - Converts the string to integer and returns it int ToInteger(){return _tstoi(m_sString);} // - Converts the string to long and returns it long ToLong(){return _tstol(m_sString);} // - Converts the string to float and returns it float ToFloat(){return (float)_tstof(m_sString);}\ // - Converts the string to double and returns it double ToDouble(){return _tstof(m_sString);} private:// - Data Members LPTSTR m_sString; // - Holds the string data int m_iLength; // - Holds the string length };
Here is some example of how to use KString:
KString str("World"); // str2 => HI WORLD! KString str2 = str.Insert("Hi ", 0).Append("!").GetUpper(); if(str2 == str.GetUpper()) printf("Yes"); str.Replace("Hi", "Hello");// str => Hello World! str2 = --str + ".";// str2 => Hello World. str = 123; int i = str.ToInteger(); str = 56.345; float f = str.ToFloat(); GetWindowsDirectory(str.GetBuffer(256), 256); str.FreeExtra();