The standard string class in STL lacks some function that I use a lot. So I wrote this class to encapsulate some of those functions.

StringUtils is a STL Template class that give you some static function that you can use on an STL string.

Three types of this class exists StringUtilsA, StringUtilsW and StringUtils.
StringUtilsA uses std::string, StringUtilsW uses std::wstring and StringUtils uses std::string or std::wstring depending on if _UNICODE is defined or not.

Everything is included in a single .h file.



bool       SplitString(const STLString& input, TCHAR delimiter , STLString& part1, STLString& part2 );
int        SplitString(const STLString& input, const STLString& delimiter, STL_VECTOR<STLString>& results);
void       Trim(STLString& str, const TCHAR* whitespace );
void       TrimLeft(STLString& str, const TCHAR* whitespace );
void       TrimRight(STLString& str, const TCHAR* whitespace );
STLString& ToLower(STLString& value);
STLString& ToUpper(STLString& value);
STLString  Format(const TCHAR* strFormat, ... );
bool       FormatString(STLString& string, const TCHAR* strFormat , ... );
void       ReplaceSubstring(STLString& str, const STLString& findStr, const STLString& replaceWith);
void       ReplaceSubstrings(STLString& str, const STLString& findStr, const STLString& replaceWith);
bool       GetLine(STL_string& str, const char** ppData);
STLString  GetSubString(const STLString& str, const STLString& beginPart, const STLString& endPart)
// Path functions
STLString& PathAppend(STLString& str, const STLString& strNewPart);
STLString& RemoveTrailingSlash(STLString& str, TCHAR ch = '\\');
STLString& AddTrailingSlash(STLString& str);
STLString& AddTrailingSlash2(STLString& str);
STLString& ReplaceFileExtension(STLString& filename, const STLString& newExtension);
bool       StripBasePath(STLString& path, const STLString& basePath);
STLString& RemoveAfterFirst(STLString& str, TCAHR ch = '\\');
STLString& RemoveBeforeFirst(STLString& str, TCAHR ch = '\\');

Usage Examples

//Trim space from the beginning and end 
STLString s = " String With Spaces ";
StringUtils::Trim(s, _T(" "));

// Split a string in two part.
STLString part1,part2;
StringUtils::SplitString( _T("Key=Value"), '=', part1, part2);
// part1 == "Key"
// part2 == "Value"

// Split a comma seperate string into strings stored in a vector
std::vector<STLString> vValues;
StringUtils::SplitString( _T("One,Two,Three,Four,Five"), ',', vValues);
DWORD dwValues = 200;
StringUtils::FormatString( str, _T("%d Items"), dwValues);
// str == "200 Items"

// Get line from buffert, Until there is not more data.
TCHAR* pLines = GetFileContent("c:\\file.txt");
while(StringUtils::GetLine(line, &pLines))
  // str is the line. pLines is moved forward until end of data (NULL).

STLString path;
StringUtils::PathAppend(path, _T("c:\\myFolder");
StringUtils::PathAppend(path, _T("sub");
StringUtils::PathAppend(path, _T("sub\\");
StringUtils::PathAppend(path, _T("file.txt");
// path == "C:\sub\file.txt


  1.0 2009-05-02  First Version
  1.1 2010-11-20  Added GetSubString(...)

( Try Multi Commander, A file manager for professionals )

Copyright (c) 2015 - Mathias Svensson