Click here to Skip to main content
15,885,890 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.5K   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 
When I try to compile an example piece of code, I get 102 errors and 24 warnings. Here's the first ones.

c:\avt\Windows\Test\StlString\string.hpp(51): warning C4018: '>' : signed/unsigned mismatch
c:\avt\Windows\Test\StlString\string.hpp(51): warning C4018: '<' : signed/unsigned mismatch
c:\avt\Windows\Test\StlString\string.hpp(119): warning C4002: too many actual parameters for macro 'va_start'
c:\avt\Windows\Test\StlString\string.hpp(58): warning C4346: 'T::reference' : dependent name is not a type
c:\avt\Windows\Test\StlString\string.hpp(58): error C2146: syntax error : missing ';' before identifier 'reference'
c:\avt\Windows\Test\StlString\string.hpp(58): error C2501: 'collection::StringC<t>::reference' : missing storage-class or type specifiers

(Btw, you need to add varargs.h to the list of include files in string.hpp.) The first actual error mentions that the number of parameters for the va_start macro is wrong. There are the correct two, but I suspect that since the first is not of a regualr type, the macro sees it differently. I'm an experienced programmer, but a relative newbie when it comes to templates. Can you offer some advice?

Also, is it necessary that the code for the template functions be all on one line? It's vertically compact, but difficult to read and manitain.
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.