Click here to Skip to main content
13,004,435 members (71,995 online)
Click here to Skip to main content
Add your own
alternative version


18 bookmarked
Posted 18 Apr 2006

A CStringT-like STL string class

, 18 Apr 2006
Rate this:
Please Sign up or sign in to vote.
A string class based on STL and that can be used like the CStringT in MFC.


In the STL library, there is a class which is called std::string, which can do a lot of outstanding extra work without any extra code. However, there's no Format() method like in MFC in its methods, and I started to write a class which could do this: job:String.

The class String can format string as if you've called the Format() method in MFC, but we do not need MFC indeed. Thus we can just use String instead of CStringT. The following member functions are supplied in this class:

  • operators (=, +, +=, CHAR(), c_str(), !=, ==, <=, >=, <, >, [])
  • ToLower(), ToUpper()
  • Mid(), Left(), Right()
  • Compare(), CompareNoCase()
  • Reverse(), Replace(), Remove(), Insert(), Delete(), Empty()
  • TrimLeft(), TrimRight()
  • Find(), FindOneOf(), ReverseFind()
  • Format()
  • GetBuffer(), GetBufferSetLength(), ReleaseBuffer(), GetLength()
  • IsEmpty()
  • GetAt(), SetAt()

How-to use this class

The following example shows how we can use this class:

#include <string>
#include "string.hpp"

String s1;
s1 = "abc";
s1 += std::string("123") + "123";
String s2 = s1.Reverse(); 
char* ptr_data = s2.CHAR();

Lack of code

I didn't add some platform-related functions in this class, because I want to use it as an platform independent class. The following list shows the CStringT member functions which were used in MFC's CStringT but not included in the class String:

  • AllocSysString()
  • SetSysString()
  • LoadString()
  • AnsiToOem()
  • OemToAnsi()


This article shows just a small usage of the code I wrote. You can use it wherever you use your CStringT in MFC.


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


About the Author

Carl Ge
Web Developer
China China
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 3 Pin
hivhiv17-Dec-10 23:01
memberhivhiv17-Dec-10 23:01 
Generalcompiler errors Pin
sborford5-Jun-06 7:28
membersborford5-Jun-06 7:28 
GeneralRe: compiler errors Pin
Carl Ge12-Jun-06 14:47
memberCarl Ge12-Jun-06 14:47 
GeneralRe: compiler errors Pin
sborford13-Jun-06 9:16
membersborford13-Jun-06 9:16 
GeneralSeems to be a Nice add on Pin
sudhir mangla19-Apr-06 18:25
membersudhir mangla19-Apr-06 18:25 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170627.1 | Last Updated 18 Apr 2006
Article Copyright 2006 by Carl Ge
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid