Click here to Skip to main content
11,434,695 members (47,205 online)
Click here to Skip to main content

A CStringT-like STL string class

, 18 Apr 2006 CPOL
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.

Introduction

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(); 
s2.Format("%d,this,%c",123,'c');
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()

Conclusion

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

License

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

Share

About the Author

Carl Ge
Web Developer
China China
No Biography provided

Comments and Discussions

 
GeneralMy vote of 3 Pin
hivhiv18-Dec-10 0:01
memberhivhiv18-Dec-10 0:01 
Generalcompiler errors Pin
sborford5-Jun-06 8:28
membersborford5-Jun-06 8:28 
GeneralRe: compiler errors Pin
Carl Ge12-Jun-06 15:47
memberCarl Ge12-Jun-06 15:47 
GeneralRe: compiler errors Pin
sborford13-Jun-06 10:16
membersborford13-Jun-06 10:16 
GeneralSeems to be a Nice add on Pin
sudhir mangla19-Apr-06 19:25
membersudhir mangla19-Apr-06 19:25 

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.150428.2 | Last Updated 18 Apr 2006
Article Copyright 2006 by Carl Ge
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid