Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C++
Article

KString class, a non MFC string class

Rate me:
Please Sign up or sign in to vote.
3.01/5 (33 votes)
15 Oct 2006CPOL 100.7K   334   11   52
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();

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) www.LogicSims.ir
Iran (Islamic Republic of) Iran (Islamic Republic of)
Hadi Dayvary graduated from Azad University in spring 2003 in Apply Mathematics.
Started Visual C++ in 1999.
He likes to program all the time.
He lives in Iran.
www.LogicSims.ir

Comments and Discussions

 
GeneralRe: Terrible! Pin
rm82219-Oct-06 21:33
rm82219-Oct-06 21:33 
GeneralRight-of-passage Pin
Midnight48917-Jul-06 11:11
Midnight48917-Jul-06 11:11 
GeneralRe: Right-of-passage Pin
roadercoder24-Jul-06 22:02
roadercoder24-Jul-06 22:02 
GeneralGood work Pin
Pharago15-Jul-06 23:28
Pharago15-Jul-06 23:28 
GeneralRe: Good work Pin
Hadi Dayvary15-Jul-06 23:47
professionalHadi Dayvary15-Jul-06 23:47 
GeneralNo magic strings Pin
Wokaiie28-May-06 8:56
Wokaiie28-May-06 8:56 
GeneralRe: No magic strings Pin
armentage27-Jul-06 17:10
armentage27-Jul-06 17:10 
GeneralRe: No magic strings Pin
Wokaiie27-Jul-06 18:58
Wokaiie27-Jul-06 18:58 
GeneralRe: No magic strings Pin
armentage29-Jul-06 7:27
armentage29-Jul-06 7:27 
GeneralRe: No magic strings Pin
Stephen Hewitt6-Sep-06 18:26
Stephen Hewitt6-Sep-06 18:26 
GeneralRe: No magic strings [modified] Pin
Stephen Hewitt6-Sep-06 18:25
Stephen Hewitt6-Sep-06 18:25 
NewsRe: KString class, a non MFC string class Pin
Roland Pibinger28-May-06 2:26
Roland Pibinger28-May-06 2:26 
GeneralRe: KString class, a non MFC string class [modified] Pin
armentage27-Jul-06 17:09
armentage27-Jul-06 17:09 
GeneralRe: KString class, a non MFC string class Pin
Stephen Hewitt6-Sep-06 18:30
Stephen Hewitt6-Sep-06 18:30 
GeneralErroneous copy constructor Pin
Jörgen Sigvardsson28-May-06 0:49
Jörgen Sigvardsson28-May-06 0:49 
QuestionRe: Erroneous copy constructor Pin
Roland Pibinger28-May-06 2:04
Roland Pibinger28-May-06 2:04 
AnswerRe: Erroneous copy constructor [modified] Pin
Hadi Dayvary28-May-06 2:09
professionalHadi Dayvary28-May-06 2:09 
Generalwell, not bad Pin
mnrz27-May-06 23:46
mnrz27-May-06 23:46 
QuestionWhy another string class? Pin
xanatos27-May-06 23:10
xanatos27-May-06 23:10 
AnswerRe: Why another string class? [modified] Pin
Hadi Dayvary27-May-06 23:17
professionalHadi Dayvary27-May-06 23:17 
GeneralRe: Why another string class? [modified] Pin
xanatos27-May-06 23:34
xanatos27-May-06 23:34 
GeneralRe: Why another string class? [modified] Pin
Hadi Dayvary28-May-06 0:43
professionalHadi Dayvary28-May-06 0:43 
GeneralRe: Why another string class? Pin
Anna-Jayne Metcalfe5-Sep-06 2:26
Anna-Jayne Metcalfe5-Sep-06 2:26 
AnswerRe: Why another string class? Pin
Roland Pibinger28-May-06 2:30
Roland Pibinger28-May-06 2:30 
AnswerRe: Why another string class? Pin
xanatos28-May-06 2:41
xanatos28-May-06 2:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.