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:
KString(void);
KString(const LPCTSTR lpszStr);
KString(const KString &sStr);
~KString(void);
KString& operator =(const KString &sStr);
KString& operator =(LPCTSTR lpszStr);
KString& operator =(const int iNum);
KString& operator =(const double fNum);
KString operator +(const KString &sStr);
KString operator +(LPCTSTR lpszStr);
KString operator +(int iNum);
KString operator +(double fNum);
KString& operator+=(const KString &sStr);
KString& operator+=(LPCTSTR lpszStr);
BOOL operator <(const KString &sStr);
BOOL operator <=(const KString &sStr);
BOOL operator >(const KString &sStr);
BOOL operator >=(const KString &sStr);
BOOL operator==(const KString &sStr);
BOOL operator==(LPCTSTR sStr);
BOOL operator!=(const KString &sStr);
BOOL operator!=(LPCTSTR sStr);
KString& operator--();
KString operator--(int);
operator LPTSTR(){return m_sString;}
TCHAR GetAt(int iPos);
int Find(TCHAR ch);
int Find(const KString &sStr);
int Find(LPCTSTR lpszStr);
int Replace(TCHAR cOld, TCHAR cNew);
int Replace(LPCTSTR sOld, LPCTSTR sNew);
int Replace(const KString &sOld, const KString &sNew);
KString Mid(int iStart, int iCount);
KString Mid(int iStart);
KString Left(int iCount);
KString Right(int iCount);
BOOL Remove(int iPos);
int Remove(int iStart, int iEnd);
int Remove(TCHAR ch);
BOOL Remove(const KString &sStr);
KString& TrimLeft(TCHAR ch = ' ');
KString& TrimRight(TCHAR ch = ' ');
KString& Trim(TCHAR ch = ' ');
KString& Insert(const KString &sStr, int iPos);
KString& Append(const KString &sStr){return *this += sStr;};
KString& MakeUpper();
KString& MakeLower();
KString& MakeReverse();
KString GetUpper();
KString GetLower();
KString GetReverse();
void Empty();
int GetLength(){return m_iLength;}
int GetSize(){return m_iLength*sizeof(TCHAR);}
BOOL IsEmpty(){return m_iLength ? FALSE : TRUE;}
LPTSTR GetBuffer(int iBuffSize);
void FreeExtra();
void Format(LPCTSTR pszCharSet, ...);
int ToInteger(){return _tstoi(m_sString);}
long ToLong(){return _tstol(m_sString);}
float ToFloat(){return (float)_tstof(m_sString);}\
double ToDouble(){return _tstof(m_sString);}
private:
LPTSTR m_sString;
int m_iLength;
};
Here is some example of how to use KString:
KString str("World");
KString str2 = str.Insert("Hi ", 0).Append("!").GetUpper();
if(str2 == str.GetUpper())
printf("Yes");
str.Replace("Hi", "Hello");
str2 = --str + ".";
str = 123;
int i = str.ToInteger();
str = 56.345;
float f = str.ToFloat();
GetWindowsDirectory(str.GetBuffer(256), 256);
str.FreeExtra();