//===============================写在前面=============================== 在标准C++中,有两种形式的字符: 分别是C语言的C风格字符串和C++标准库中的string类。 其中C风格的字符串以字符数组 char*或者char[]来实现, 可是功能很弱,并且很不安全。 标准C++采用类模板的形式来封装字符串,建议使用。 头文件说明: C风格字符串: #include <string.h> //这是C语言和非标准C++用的老式的头文件 #include <cstring> //这是标准C++用的新式的头文件 //string.h和cstring在功能上基本相似 //只是后者的内容都放在命名空间std中 标准C++的string类头文件 #include <string> //全部内容放在命名空间std中 注1: string.h和cstring基本相同,后者是前者的替代版本。 可是和string彻底是两个不一样的头文件。 注2:在MFC中还有一个功能更强大的类CString类来处理字符串 //========================================================================== 对于C风格的字符串,操做很不安全, 包括C++提供的两个字符串链接函数strcat和strncat。 所以我写了多个函数用于字符串链接,并放在命名空间TYCppStdLib中。 其中我写的strcat和一样是不安全的。 但相比C++提供的strcat要好一点,避免了由于空指针而程序崩溃的状况。 另外我写的五个重载函数strcatEx利用安全的string为中间媒介,安全可靠。 //==========================================================================
/*- ========================================================== * 文件名 :TYStringAndCharFunc.h * 开发人员:袁培荣 * 当前版本:1.0.0.2595 * 建立时间:2012-04-23 * 修改时间:2012-04-30 * 功能说明:字符串处理函数 * 版权说明:版权全部 袁培荣 YuanPeirong * 编译环境:Windows 7(x64) SP1 简体中文专业版 * 编译器: Visual Studio 2010 SP1(中文旗舰版) MinGW 2011118 Visual C++ 6.0 SP6(中文企业版) - ==========================================================*/ #ifndef TYStringAndCharFunc_H_TYCppStdLib #define TYStringAndCharFunc_H_TYCppStdLib #ifdef TYStringAndCharFunc_DLL_API #else #define TYStringAndCharFunc_DLL_API _declspec(dllimport) #endif #include <string> using namespace std; namespace TYCppStdLib { //String //Char //获取字符的ACSII码 TYStringAndCharFunc_DLL_API int Asc(char ch); TYStringAndCharFunc_DLL_API int Asc(char *ch); //StringAndChar //字符串链接 TYStringAndCharFunc_DLL_API void strcat( char *s, //C风格字符串,链接结果存于s中 char *d //C风格字符串 ); //与std中的strcat同名,调用时加TYCppStdLib:: 调试模式出错 TYStringAndCharFunc_DLL_API string strcatEx( const char *c1, //C风格字符串 const char *c2 //C风格字符串 ); //以string类型返回c1和c2链接后的字符串 TYStringAndCharFunc_DLL_API string strcatEx( const char *c1, //C风格字符串 const string &s2 //C++string字符串 ); //以string类型返回c1和s2链接后的字符串 TYStringAndCharFunc_DLL_API string strcatEx( const string &s1, //C++string字符串 const char *c2 //C风格字符串 ); //以string类型返回s1和c2链接后的字符串 TYStringAndCharFunc_DLL_API void strcatEx( const char *c1, //C风格字符串 string &s2 //C++string字符串 ); //将c1和s2链接后的字符串存于s2中 TYStringAndCharFunc_DLL_API void strcatEx( string &s1, //C++string字符串 const char *c2 //C风格字符串 ); //将s1和c2链接后的字符串存于s1中 } #endif
/*- ========================================================== * 文件名 :TYStringAndCharFunc.cpp * 开发人员:袁培荣 * 当前版本:1.0.0.2595 * 建立时间:2012-04-23 * 修改时间:2012-04-30 * 功能说明:字符串处理函数 * 版权说明:版权全部 袁培荣 YuanPeirong * 编译环境:Windows 7(x64) SP1 简体中文专业版 * 编译器: Visual Studio 2010 SP1(中文旗舰版) MinGW 2011118 Visual C++ 6.0 SP6(中文企业版) - ==========================================================*/ #ifndef TYStringAndCharFunc_DLL_ForAPI #define TYStringAndCharFunc_DLL_ForAPI #define TYStringAndCharFunc_DLL_API _declspec(dllexport) #endif #include "../../Include/StringAndChar/TYStringAndCharFunc.h" #include <cstring> using namespace std; void TYCppStdLib::strcat(char *s, char *d) { int i, ls, ld; if(!s || !d) return; ls=strlen(s); ld=strlen(d); for(i=0;i<=ld;i++) s[ls+i]=d[i]; } string TYCppStdLib::strcatEx(const char *c1, const char *c2) { string s1="",s2=""; if(c1) s1=c1; if(c2) s2=c2; return (s1+s2); } string TYCppStdLib::strcatEx(const char *c1, const string &s2) { string s1=""; if(c1) s1=c1; return (s1+s2); } string TYCppStdLib::strcatEx(const string &s1, const char *c2) { string s2=""; if(c2) s2=c2; return (s1+s2); } void TYCppStdLib::strcatEx(const char *c1, string &s2) { string s1=""; if(c1) s1=c1; s2=s1+s2; } void TYCppStdLib::strcatEx(string &s1, const char *c2) { string s2=""; if(c2) s2=c2; s1=s1+s2; }