VC++中使用频率最高的字符串莫过于std::string和CString了,但是std::string相比较CString而言,少了字符串格式化功能,于是扩展一下,方便以后使用:
// xstring.h
#pragma once#include
#include
#include namespace SW{templateclass 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 &#61; static_cast (-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 &#61; 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 &#61; _Alloc()) :basic_string<_CharT, _Traits, _Alloc> (__s, __n, __a){}string(const _CharT* __s, const _Alloc& __a &#61; _Alloc()) :basic_string<_CharT, _Traits, _Alloc> (__s, __a){}string(size_type __n, _CharT __c, const _Alloc& __a &#61; _Alloc()) :basic_string<_CharT, _Traits, _Alloc> (__n, __c, __a){}templatestring(_InputIterator __beg, _InputIterator __end, const _Alloc& __a &#61;_Alloc()) :basic_string<_CharT, _Traits, _Alloc> (__beg, __end, __a){}string& operator&#61;(const string& __str){return this->assign(__str);}string& operator&#61;(const _CharT* __s){this->assign(__s);return *this;}string& operator&#61;(_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(" ") &#43; 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] &#61; { 0 };memset(szBuffer, 0x00, sizeof(szBuffer));try{vsnprintf_s(szBuffer, 1000, _format, args);} catch (...){cout <<"ERROR: format the string failed..." <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..." <append(szBuffer);return *this;}inline string &Replace(string oldStr, string newStr){string::size_type pos &#61; 0;string::size_type srcLen &#61; oldStr.size();string::size_type desLen &#61; newStr.size();pos &#61; find(oldStr, pos);while ((pos !&#61; string::npos)){strSrc.replace(pos, srcLen, newStr);pos &#61; strSrc.find(oldStr, (pos &#43; desLen));}return *this;}std::vector ExtractSubString(string separateStr, bool repeatedCharIgnored){string sSrc &#61; *this;std::vector resultStringVector;size_t pos &#61; sSrc.find(separateStr.c_str());string subString &#61; "";while (pos !&#61; string::npos){subString &#61; sSrc.substr(0, pos);if (!subString.empty() || !repeatedCharIgnored){resultStringVector.push_back(subString);}sSrc.erase(sSrc.begin(), sSrc.begin() &#43; pos &#43; separateStr.length());pos &#61; sSrc.find(separateStr.c_str());}subString &#61; sSrc;if (!subString.empty() || !repeatedCharIgnored){resultStringVector.push_back(subString);}return resultStringVector;}};using string &#61; basic_string, allocator>;using wstring &#61; basic_string, allocator>;using u16string &#61; basic_string, allocator>;using u32string &#61; basic_string, allocator>;
}