//===============================寫在前面=============================== 在標準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; }