Click here to Skip to main content
15,888,089 members
Articles / Programming Languages / C++

A CStringT-like STL string class

Rate me:
Please Sign up or sign in to vote.
2.63/5 (11 votes)
18 Apr 2006CPOL1 min read 43.6K   418   18   5
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:

C++
#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)


Written By
Web Developer
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 3 Pin
hivhiv17-Dec-10 23:01
hivhiv17-Dec-10 23:01 
Generalcompiler errors Pin
sborford5-Jun-06 7:28
sborford5-Jun-06 7:28 
GeneralRe: compiler errors Pin
Carl Ge12-Jun-06 14:47
Carl Ge12-Jun-06 14:47 
GeneralRe: compiler errors Pin
sborford13-Jun-06 9:16
sborford13-Jun-06 9:16 
GeneralSeems to be a Nice add on Pin
Sudhir Mangla19-Apr-06 18:25
professionalSudhir 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.