Click here to Skip to main content
15,893,622 members
Articles / Desktop Programming / MFC

Useful CString Manipulation Functions

Rate me:
Please Sign up or sign in to vote.
2.87/5 (5 votes)
31 Jan 2007CPOL2 min read 44.2K   257   17   6
A set of functions to perform the tasks that CString fails to address properly
Sample image

Introduction

Until very recently, I've been mostly using my own string class and it worked OK. But then I felt the urge to stick more to the standard and decided to use CString for all string manipulations, only it doesn't have some of the functions I used so often in my string class. At first, I decided to derive my own class from CString, but quickly dropped this idea because the solution would be far from flexible, especially with dozens of constructors available in CStringT. Instead, I made several global functions which do the things that CString doesn't - this is what StringLib.h is.

Using the Code

StringLib is best used to supplement CString. That is, you could use CString as usual in your application, and call some of StringLib's functions from time to time in order to perform special tasks. This can save you the time needed to write trivial and often used string manipulation code.

So what can StringLib do anyway?

Path Management

  • C++
    bool IsRelativePath(const TCHAR* str);

    Determines if the path that was passed to it is relative or absolute. Network paths are considered absolute.

  • C++
    bool RelativeToAbsolute(CString& str, HMODULE base = 0);
    bool RelativeToAbsolute(CString& str, const TCHAR* base);

    Converts the path contained in the string from relative (to the given module or other file) into absolute. This one's very useful because you can get the full path to any file in your application's directory with just one line of code.

  • C++
    void TailBackslash(CString& str);

    Adds a backslash character to the end of the string if there isn't one already.

Tokenizing

  • C++
    int GetTokenCount(const TCHAR* str, const TCHAR* delimiters);

    Determines the number of tokens inside the string.

  • C++
    CString GetToken(const TCHAR* str, const TCHAR* delimiters, int index);

    Retrieves the token with the given zero-based index from the string. It's rather inefficient due to the nature of the operation, so be warned.

  • C++
    bool TrimRightToken(CString& str, const TCHAR* delimiters);

    Removes the rightmost token in the given string.

  • C++
    void Explode(const TCHAR* str, const TCHAR* delimiters, CStringArray& arr);

    Converts the given string into an array of tokens.

  • C++
    CString Implode(TCHAR delimiter, const CStringArray& arr);

    Converts the given array of tokens into a string.

Integers

  • C++
    int ToInt(const TCHAR* str);

    Converts from string to integer. Looks much better than this ugly _ttoi.

  • C++
    CString ToCString(int num);

    Converts from integer to string. Much shorter than Format.

Points of Interest

The code may look a bit inefficient in some of the functions. That's because it is. However, the reason for this inefficiency is that these functions can hardly be implemented much, much better due to the nature of the problems that they solve.

But just like many other developers, I believe code reuse and lower maintenance costs always warrant a trifle inefficient code. It's exactly what these functions do - help in making your code more readable by allowing you to concentrate on the task at hand rather than on string tokenizing, for instance. No wonder this comes at a price.

Disclaimer

Of course you may use this code as you please, but do so at your own risk, although there doesn't seem to be any immediate risk in using it.

Happy coding!

History

  • 31st January, 2007: Initial post

License

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


Written By
Software Developer (Senior) Ramka Ltd.
Ukraine Ukraine
I'm a Software Developer / Architect from Kiev, Ukraine.

In the line of duty, I mostly use C/C++ (it's also by far my favorite) and C# (although it's a love-hate relationship).

I enjoy a challenging task to really make me scratch the back of my head every now and then.

Comments and Discussions

 
GeneralThanks Pin
Erik22218-Jun-07 22:39
Erik22218-Jun-07 22:39 
GeneralRe: Thanks Pin
Dennis Gourjii19-Jun-07 5:40
Dennis Gourjii19-Jun-07 5:40 
NewsRelative/network path handling Pin
Ravi Bhavnani1-Feb-07 3:20
professionalRavi Bhavnani1-Feb-07 3:20 
GeneralRe: Relative/network path handling Pin
Dennis Gourjii1-Feb-07 3:25
Dennis Gourjii1-Feb-07 3:25 
GeneralRe: Relative/network path handling Pin
Panic2k34-Nov-09 20:23
Panic2k34-Nov-09 20:23 
GeneralRe: Relative/network path handling Pin
Dennis Gourjii5-Nov-09 10:47
Dennis Gourjii5-Nov-09 10:47 

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.