Click here to Skip to main content
15,867,308 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.4K   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

 
GeneralMemory leak in "Format" method Pin
Tomice29-Oct-10 11:04
Tomice29-Oct-10 11:04 
GeneralRe: Memory leak in "Format" method Pin
Hadi Dayvary29-Oct-10 22:05
professionalHadi Dayvary29-Oct-10 22:05 
GeneralMid optimization [modified] Pin
Tomice28-Oct-10 3:57
Tomice28-Oct-10 3:57 
GeneralRe: Mid optimization Pin
Hadi Dayvary29-Oct-10 21:01
professionalHadi Dayvary29-Oct-10 21:01 
GeneralExcellent Work!!! Pin
raajaak26-Jun-07 20:42
raajaak26-Jun-07 20:42 
GeneralRe: Excellent Work!!! Pin
Hadi Dayvary26-Jun-07 21:01
professionalHadi Dayvary26-Jun-07 21:01 
QuestionCan you help me to do Trackbar (Sliderbar) in win32? Pin
raajaak26-Jun-07 21:18
raajaak26-Jun-07 21:18 
AnswerRe: Can you help me to do Trackbar (Sliderbar) in win32? Pin
Hadi Dayvary26-Jun-07 23:09
professionalHadi Dayvary26-Jun-07 23:09 
GeneralRe: Can you help me to do Trackbar (Sliderbar) in win32? Pin
raajaak26-Jun-07 23:24
raajaak26-Jun-07 23:24 
GeneralHelp: How can i refresh windows mobile? Pin
raajaak27-Jun-07 19:40
raajaak27-Jun-07 19:40 
GeneralRe: Help: How can i refresh windows mobile? Pin
Hadi Dayvary27-Jun-07 19:59
professionalHadi Dayvary27-Jun-07 19:59 
GeneralCString is not only MFC Pin
Zodiacon18-Oct-06 7:46
Zodiacon18-Oct-06 7:46 
Generalmissing Array Pin
Taulie7-Sep-06 22:44
Taulie7-Sep-06 22:44 
GeneralRe: missing Array Pin
Hadi Dayvary9-Sep-06 20:34
professionalHadi Dayvary9-Sep-06 20:34 
GeneralSuggestion - use std::string Pin
Robert W.5-Sep-06 3:22
Robert W.5-Sep-06 3:22 
GeneralRe: Suggestion - use std::string Pin
John M. Drescher5-Sep-06 4:15
John M. Drescher5-Sep-06 4:15 
GeneralRe: Suggestion - use std::string Pin
Maximilien16-Oct-06 6:56
Maximilien16-Oct-06 6:56 
GeneralRe: Suggestion - use std::string Pin
John M. Drescher18-Oct-06 4:33
John M. Drescher18-Oct-06 4:33 
GeneralYou could have written a util class Pin
armentage27-Jul-06 17:20
armentage27-Jul-06 17:20 
QuestionCustomization? Pin
armentage27-Jul-06 17:12
armentage27-Jul-06 17:12 
GeneralLength of data Pin
MartinK17-Jul-06 21:50
MartinK17-Jul-06 21:50 
GeneralTerrible! Pin
rm82217-Jul-06 20:39
rm82217-Jul-06 20:39 
GeneralRe: Terrible! Pin
Skizmo24-Jul-06 22:23
Skizmo24-Jul-06 22:23 
GeneralRe: Terrible! Pin
rm82224-Jul-06 22:57
rm82224-Jul-06 22:57 
GeneralRe: Terrible! Pin
benben19-Oct-06 15:32
benben19-Oct-06 15:32 

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.