VC++中使用频率最高的字符串莫过于std::string和CString了,但是std::string相比较CString而言,少了字符串格式化功能,于是扩展一下,方便以后使用:
// xstring.h
#pragma once
#include <stdio.h>
#include <iostream>
#include <stdarg.h>
namespace SW{
template<typename _CharT, typename _Traits, typename _Alloc>
class string: public basic_string<_CharT, _Traits, _Alloc>
{
public:
typedef typename _Alloc::template rebind<_CharT>::other _CharT_alloc_type;
typedef typename _CharT_alloc_type::size_type size_type;
static const size_type npos = static_cast<size_type> (-1);
inline string() :
basic_string<_CharT, _Traits, _Alloc> ()
{
}
explicit string(const _Alloc& __a) :
basic_string<_CharT, _Traits, _Alloc> ()
{
}
string(const string& __str) :
basic_string<_CharT, _Traits, _Alloc> (__str)
{
}
string(const string& __str, size_type __pos, size_type __n = npos) :
basic_string<_CharT, _Traits, _Alloc> (__str, __pos, npos)
{
}
string(const string& __str, size_type __pos, size_type __n,
const _Alloc& __a) :
basic_string<_CharT, _Traits, _Alloc> (__str, __pos, __n, __a)
{
}
string(const _CharT* __s, size_type __n, const _Alloc& __a = _Alloc()) :
basic_string<_CharT, _Traits, _Alloc> (__s, __n, __a)
{
}
string(const _CharT* __s, const _Alloc& __a = _Alloc()) :
basic_string<_CharT, _Traits, _Alloc> (__s, __a)
{
}
string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc()) :
basic_string<_CharT, _Traits, _Alloc> (__n, __c, __a)
{
}
template<class _InputIterator>
string(_InputIterator __beg, _InputIterator __end, const _Alloc& __a =
_Alloc()) :
basic_string<_CharT, _Traits, _Alloc> (__beg, __end, __a)
{
}
string& operator=(const string& __str)
{
return this->assign(__str);
}
string& operator=(const _CharT* __s)
{
this->assign(__s);
return *this;
}
string& operator=(_CharT __c)
{
this->assign(1, __c);
return *this;
}
inline string& LeftTrim()
{
this->erase(0, this->find_first_not_of(" "));
return *this;
}
inline string& RightTrim()
{
this->erase(this->find_last_not_of(" ") + 1);
return *this;
}
inline string& Trim()
{
LeftTrim();
RightTrim();
return *this;
}
inline string &Format(const char *_format, ...)
{
va_list args;
va_start(args, _format);
char szBuffer[1000] = { 0 };
memset(szBuffer, 0x00, sizeof(szBuffer));
try
{
vsnprintf_s(szBuffer, 1000, _format, args);
} catch (...)
{
cout << "ERROR: format the string failed..." << endl;
return *this;
}
va_end(args);
this->assign(szBuffer);
return *this;
}
inline string &AppendFormat(const char *_format, ...)
{
char szBuffer[1000];
memset(szBuffer, 0x00, sizeof(szBuffer));
va_list ap;
va_start(ap, _format);
try
{
vsnprintf(szBuffer, 1000, _format, ap);
} catch (...)
{
cout << "ERROR: format the string failed..." << endl;
return *this;
}
va_end(ap);
this->append(szBuffer);
return *this;
}
inline string &Replace(string oldStr, string newStr)
{
string::size_type pos = 0;
string::size_type srcLen = oldStr.size();
string::size_type desLen = newStr.size();
pos = find(oldStr, pos);
while ((pos != string::npos))
{
strSrc.replace(pos, srcLen, newStr);
pos = strSrc.find(oldStr, (pos + desLen));
}
return *this;
}
std::vector<string> ExtractSubString(string separateStr, bool repeatedCharIgnored)
{
string sSrc = *this;
std::vector<string> resultStringVector;
size_t pos = sSrc.find(separateStr.c_str());
string subString = "";
while (pos != string::npos)
{
subString = sSrc.substr(0, pos);
if (!subString.empty() || !repeatedCharIgnored)
{
resultStringVector.push_back(subString);
}
sSrc.erase(sSrc.begin(), sSrc.begin() + pos + separateStr.length());
pos = sSrc.find(separateStr.c_str());
}
subString = sSrc;
if (!subString.empty() || !repeatedCharIgnored)
{
resultStringVector.push_back(subString);
}
return resultStringVector;
}
};
using string = basic_string<char, char_traits<char>, allocator<char>>;
using wstring = basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t>>;
using u16string = basic_string<char16_t, char_traits<char16_t>, allocator<char16_t>>;
using u32string = basic_string<char32_t, char_traits<char32_t>, allocator<char32_t>>;
}