Click here to Skip to main content
15,907,183 members
Articles / Programming Languages / C++
Article

String Utils

Rate me:
Please Sign up or sign in to vote.
1.34/5 (14 votes)
7 May 2006GPL31 min read 32.4K   502   8   3
Simple utilities to manage strings. Specially devoted to VB backgrounders

Introduction

To those used to manage strings in VB, the existance of functions like Split, Join, Replace and the like makes programming a bliss. C++ programmers don't have those "built in" facilities. For them, the present set of functions attemps to accomplish the same goal.

Matter gets worse for C++ programmers because besides the lack of those functions, they usually have to struggle with many kinds of strings (C like strings, STD strings, BSTRS, CStrings, etc). The idea is to bring a unified set of tools to cope with them all. Thus, the present tools resorts to the use of templates, allowing this way to receive as input anything that can be used to construct a std::string. There are also a couple of template specializations to deal with single charactters. A secondary goal is to show the use of template functions and namespaces.

Using the code

The following code snippet should be self explanatory:

//StringUtils::Split usage
const char* szToSplit = "Hello|World|from|String|Utils";
std::vector<std::string> v = StringUtils::Split(szToSplit, "|");

for (size_t i = 0; i < v.size(); i++)
      std::cout << v[i] << std::endl;

//StringUtils::Join usage
std::string s = StringUtils::Join(v,"#");
std::cout << s << std::endl;
//should produce: Hello#World#from#String#Utils


Enough words for now, all you've gotta do is to #include "stringutils.h" in your source code. Happy templating!

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Web Developer
Argentina Argentina
Born in Rosario, Argentina.
Interested in system programming, Internet apps, real time apps. Recently became Python evangelist.
Favorite tools: Python, C++, C#, Java (in that precise order)
Fluent in SQL, COM, ATL-WTL
Enjoy playing chess games, specially tournament games, and walking when I'm not programming.

Comments and Discussions

 
GeneralLeft() Right() Mid() and Mid()= Pin
hpfx6-Aug-07 23:33
hpfx6-Aug-07 23:33 
QuestionBug in Split? Pin
Anders Dalvander7-May-06 23:41
Anders Dalvander7-May-06 23:41 
AnswerRe: Bug in Split? Pin
Ernesto Savoretti8-May-06 10:43
Ernesto Savoretti8-May-06 10:43 
Well, you know, what other people calls a bug Redmontonians call a feature... Smile | :)
No, seriously, Redmontonians are innocent this time, the intent was to produce the same effect that the corresponding facility has in VB, so... it's a bug.
In VB (you can test it using VBA through some Office product) when you call Split("") you get undefined behaviour, expressed in no string array at all.
A fix is underway.

Ernesto Savoretti

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.