Click here to Skip to main content
12,951,034 members (56,095 online)
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 31 Jan 2007

Useful CString Manipulation Functions

, 31 Jan 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
A set of functions to perform the tasks that CString fails to address properly
Sample image


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

  • bool IsRelativePath(const TCHAR* str);

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

  • 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.

  • void TailBackslash(CString& str);

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


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

    Determines the number of tokens inside the string.

  • 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.

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

    Removes the rightmost token in the given string.

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

    Converts the given string into an array of tokens.

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

    Converts the given array of tokens into a string.


  • int ToInt(const TCHAR* str);

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

  • 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.


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!


  • 31st January, 2007: Initial post


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


About the Author

Dennis Gourjii
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.

You may also be interested in...


Comments and Discussions

GeneralThanks Pin
Erik22218-Jun-07 22:39
memberErik22218-Jun-07 22:39 
GeneralRe: Thanks Pin
Denis Gourjii19-Jun-07 5:40
memberDenis Gourjii19-Jun-07 5:40 
NewsRelative/network path handling Pin
Ravi Bhavnani1-Feb-07 3:20
memberRavi Bhavnani1-Feb-07 3:20 
GeneralRe: Relative/network path handling Pin
Denis Gourjii1-Feb-07 3:25
memberDenis Gourjii1-Feb-07 3:25 
GeneralRe: Relative/network path handling Pin
Panic2k34-Nov-09 20:23
memberPanic2k34-Nov-09 20:23 
GeneralRe: Relative/network path handling Pin
Denis Gourjii5-Nov-09 10:47
memberDenis 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 1 Feb 2007
Article Copyright 2007 by Dennis Gourjii
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid