Click here to Skip to main content
Click here to Skip to main content

KString class, a non MFC string class

, 16 Oct 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Hadi Dayvary
Software Developer 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
Follow on   Google+

Comments and Discussions

 
GeneralMemory leak in "Format" method PinmemberTomice29-Oct-10 12:04 
GeneralRe: Memory leak in "Format" method PinmemberHadi Dayvary29-Oct-10 23:05 
Hi
Thank you again,
 
_vsctprintf is a good function and I did not know about it.
These years I'm just working on a math program and UI codes and I did not get a chance to update this article.
 
But thank you for your tips.
 
Regrads,
Hadi
www.logicsims.ir

GeneralMid optimization [modified] PinmemberTomice28-Oct-10 4:57 
GeneralRe: Mid optimization PinmemberHadi Dayvary29-Oct-10 22:01 
GeneralExcellent Work!!! Pinmemberraajaak26-Jun-07 21:42 
GeneralRe: Excellent Work!!! PinmemberHadi Dayvary26-Jun-07 22:01 
QuestionCan you help me to do Trackbar (Sliderbar) in win32? Pinmemberraajaak26-Jun-07 22:18 
AnswerRe: Can you help me to do Trackbar (Sliderbar) in win32? PinmemberHadi Dayvary27-Jun-07 0:09 
GeneralRe: Can you help me to do Trackbar (Sliderbar) in win32? Pinmemberraajaak27-Jun-07 0:24 
GeneralHelp: How can i refresh windows mobile? Pinmemberraajaak27-Jun-07 20:40 
GeneralRe: Help: How can i refresh windows mobile? PinmemberHadi Dayvary27-Jun-07 20:59 
GeneralCString is not only MFC PinmemberZodiacon18-Oct-06 8:46 
Generalmissing Array Pinmembergrandmasta17-Sep-06 23:44 
GeneralRe: missing Array PinmemberHadi Dayvary9-Sep-06 21:34 
GeneralSuggestion - use std::string PinmemberRobert W.5-Sep-06 4:22 
GeneralRe: Suggestion - use std::string PinmemberJohn M. Drescher5-Sep-06 5:15 
GeneralRe: Suggestion - use std::string PinmemberMaximilien16-Oct-06 7:56 
GeneralRe: Suggestion - use std::string PinmemberJohn M. Drescher18-Oct-06 5:33 
GeneralYou could have written a util class Pinmemberarmentage27-Jul-06 18:20 
QuestionCustomization? Pinmemberarmentage27-Jul-06 18:12 
GeneralLength of data PinmemberMartinK17-Jul-06 22:50 
GeneralTerrible! Pinmemberrm82217-Jul-06 21:39 
GeneralRe: Terrible! PinmemberSkizmo24-Jul-06 23:23 
GeneralRe: Terrible! Pinmemberrm82224-Jul-06 23:57 
GeneralRe: Terrible! Pinmemberbenben19-Oct-06 16:32 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141220.1 | Last Updated 16 Oct 2006
Article Copyright 2006 by Hadi Dayvary
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid