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

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.

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 Pinmemberhivhiv17-Dec-10 23:01 
Generalcompiler errors Pinmembersborford5-Jun-06 7:28 
GeneralRe: compiler errors PinmemberCarl Ge12-Jun-06 14:47 
GeneralRe: compiler errors Pinmembersborford13-Jun-06 9:16 
GeneralSeems to be a Nice add on Pinmembersudhir mangla19-Apr-06 18: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 | Mobile
Web04 | 2.8.140821.2 | Last Updated 18 Apr 2006
Article Copyright 2006 by Carl Ge
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid