厚积薄发—C++标准库string类的扩展(暂行稿)

 1 //===============================写在前面===============================
 2 在标准C++中,有两种形式的字符:
 3 分别是C语言的C风格字符串和C++标准库中的string类。
 4 
 5 其中C风格的字符串以字符数组 char*或者char[]来实现,
 6 但是功能很弱,而且很不安全。
 7 
 8 标准C++采用类模板的形式来封装字符串,建议使用。
 9 
10 头文件说明:
11 
12 C风格字符串: 
13 #include <string.h> //这是C语言和非标准C++用的老式的头文件
14 #include <cstring>  //这是标准C++用的新式的头文件
15 //string.h和cstring在功能上基本类似
16 //只是后者的内容都放在命名空间std中
17 
18 标准C++的string类头文件
19 #include <string> //所有内容放在命名空间std中
20 
21 注1:
22     string.h和cstring基本相同,后者是前者的替代版本。
23     但是和string完全是两个不同的头文件。
24 
25 注2:在MFC中还有一个功能更强大的类CString类来处理字符串
26 //==========================================================================
27 经过多日准备,现在发布我写的C++标准库string类的扩展stringEx类(暂时稿)
28 之所以称为暂行稿的原因:
29 1.
30   现在发布的功能还比较少,有待增加
31 2.
32   stringEx直接将string的对象作为成员变量进行string类的扩展,
33   这种做法是正确的,但是,却不是很好,因此只是暂行。
34   我写的C++代码不但要正确,更要追求C++标准,还要符合高级的C++编程规范。
35   虽然不标准和不和高级规范都是正确的,但不能让我满足。
36 3.
37   C++的高级编程规范向程序员建议:不要将基类的对象作为派生类的成员变量。
38 //==========================================================================
39 
40 源代码如下:
  1 /*- ==========================================================
  2 *     文件名  :stringEx.h
  3 *     开发人员:袁培荣
  4 *     当前版本:1.0.0.2595
  5 *     创建时间:2012-04-22
  6 *     修改时间:2012-05-08
  7 *     功能说明:C++标准库string类的扩展(暂行稿)
  8 *     版权说明:版权所有 袁培荣 YuanPeirong 
  9 *     编译环境:Windows 7(x64) SP1 简体中文专业版
 10 *     编译器:  Visual Studio 2010 SP1(中文旗舰版)
 11                 MinGW 2011118
 12                 Visual C++ 6.0 SP6(中文企业版)
 13 - ==========================================================*/
 14 
 15 #ifndef stringEx_H_TYCppStdLib
 16 #define stringEx_H_TYCppStdLib
 17 
 18 #ifdef stringEx_DLL_API
 19 #else
 20 #define stringEx_DLL_API _declspec(dllimport)
 21 #endif
 22 
 23 #include <string>
 24 
 25 using std::string;
 26 
 27 namespace TYCppStdLib
 28 {   
 29     class stringEx_DLL_API stringEx : public string
 30     {
 31     private:
 32         string m_strEx;
 33     public:
 34         //构造函数
 35         
 36             //为本类和基类共同构造
 37         stringEx();        
 38             //为本类构造
 39         stringEx(string str);       
 40             //为基类string构造
 41         stringEx(const char *str);  
 42         stringEx(size_type length, char ch);                      
 43         stringEx(const char *str, size_type length);              
 44         stringEx(string &str, size_type index, size_type length); 
 45         stringEx(const_iterator start, const_iterator end);
 46         
 47         //虚析构函数
 48         virtual ~stringEx();
 49 
 50         //同步对象本身和m_strEx的值
 51         void Synchronous(bool positive=true); 
 52 
 53         //获取m_strEx
 54         string GetString();
 55         void GetString(string &getString);
 56         
 57         //设置m_strEx
 58         void SetString(string setString);
 59         
 60         //内联函数(是否数字,字母转大写,字母转小写)
 61         inline bool IsDigitInline(char *buf)
 62         {
 63             return((*buf & 0x10) ? 1 : (*buf =='.'));
 64         }
 65         inline char CharToUpperInline(unsigned char c)
 66         {
 67             return ((c >= 'a' && c <= 'z') ? c-('a'-'A') : c );
 68         }
 69         inline char CharToLowerInline(unsigned char c)
 70         {
 71             return ((c >= 'A' && c <= 'Z') ? c+('a'-'A') : c );
 72         }
 73         
 74         // bool IsDigit(char *buf);
 75         // char CharToUpper(unsigned char c);
 76         // char CharToLower(unsigned char c);
 77         
 78         //判断字符串是不是数字
 79         bool IsNumeric();
 80         bool IsNumeric(string stString);
 81         bool IsNumeric(string *stString);
 82         //bool IsNumeric(string &stString);
 83         
 84         //字符串中所有字母转为大写形式
 85         string Upcase(bool UpMe=false);
 86         string Upcase(string stString);
 87         void Upcase(string *stString);
 88         //void Upcase(string &stString);
 89         
 90         //字符串中所有字母转为小写形式
 91         string Lowercase(bool LowMe=false);
 92         string Lowercase(string stString);
 93         void Lowercase(string *stString);
 94         //void Lowercase(string &stString);
 95 
 96         //去掉左边指定字符串
 97         string LTrim(bool LTrMe=false, string LTrStr=" ");
 98         string LTrim(string stString, string LTrStr=" ");
 99         void LTrim(string *stString, string LTrStr=" ");
100         //void LTrim(string &stString, string LTrStr=" ");
101 
102         //去掉右边指定字符串
103         string RTrim(bool RTrMe=false, string RTrStr=" ");
104         string RTrim(string stString, string RTrStr=" ");
105         void RTrim(string *stString, string RTrStr=" ");
106         //void RTrim(string &stString, string RTrStr=" ");
107         
108         //去掉两边指定字符串
109         string Trim(bool TrMe=false, string TrStr=" ");
110         string Trim(string stString, string TrStr=" ");
111         void Trim(string *stString, string TrStr=" ");
112         //void Trim(string &stString, string TrStr=" ");
113     };
114 }
115 
116 #endif
  1 /*- ==========================================================
  2 *     文件名  :stringEx.cpp
  3 *     开发人员:袁培荣
  4 *     当前版本:1.0.0.2595
  5 *     创建时间:2012-04-22
  6 *     修改时间:2012-05-08
  7 *     功能说明:C++标准库string类的扩展(暂行稿)
  8 *     版权说明:版权所有 袁培荣 YuanPeirong 
  9 *     编译环境:Windows 7(x64) SP1 简体中文专业版
 10 *     编译器:  Visual Studio 2010 SP1(中文旗舰版)
 11                 MinGW 2011118
 12                 Visual C++ 6.0 SP6(中文企业版)
 13 - ==========================================================*/
 14 
 15 #ifndef stringEx_DLL_ForAPI
 16 #define stringEx_DLL_ForAPI
 17 
 18 #define stringEx_DLL_API _declspec(dllexport)
 19 
 20 #endif
 21 
 22 //#include "http://www.cnblogs.com/Include/stringEx.h"
 23 
 24 //构造函数
 25 TYCppStdLib::stringEx::stringEx()
 26 {
 27     m_strEx="";
 28 }
 29 
 30 TYCppStdLib::stringEx::stringEx(string str)
 31 {
 32     m_strEx=str;
 33     //m_strEx.assign(str);
 34     *this=m_strEx;
 35 }
 36 
 37 TYCppStdLib::stringEx::stringEx(const char *str ):string(str)
 38 {
 39     m_strEx=*this;
 40 }
 41 
 42 TYCppStdLib::stringEx::stringEx(
 43     size_type length, 
 44     char ch
 45     ):string(length, ch)
 46 {
 47     m_strEx=*this;
 48 }
 49 
 50 TYCppStdLib::stringEx::stringEx(
 51     const char *str, 
 52     size_type length
 53     ):string(str, length)
 54 {
 55     m_strEx=*this;
 56 }
 57 
 58 TYCppStdLib::stringEx::stringEx(
 59     string &str, 
 60     size_type index, 
 61     size_type length
 62     ):string(str, index, length)
 63 {
 64     m_strEx=*this;
 65 }
 66 
 67 TYCppStdLib::stringEx::stringEx(
 68     const_iterator start, 
 69     const_iterator end
 70     ):string(start, end)
 71 {
 72     m_strEx=*this;
 73 }
 74 
 75 //虚析构函数
 76 TYCppStdLib::stringEx::~stringEx()
 77 {
 78     //空函数
 79 }
 80 
 81 //同步对象本身和m_strEx的值
 82 void TYCppStdLib::stringEx::Synchronous(bool positive)
 83 {
 84     if(true==positive)
 85         m_strEx=*this;
 86     else
 87         *this=m_strEx;
 88 }
 89 
 90 //获取m_strEx
 91 string TYCppStdLib::stringEx::GetString()
 92 {
 93     string getString=m_strEx;
 94     return getString;
 95 }
 96 
 97 void TYCppStdLib::stringEx::GetString(string &getString)
 98 {
 99     getString=m_strEx;
100     //getString.assign(m_strEx);
101 }
102 
103 //设置m_strEx
104 void TYCppStdLib::stringEx::SetString(string setString)
105 {
106     m_strEx=setString;
107     //m_strEx.assign(setString);
108     Synchronous(false);
109 }
110 
111 //判断字符串是不是数字
112 bool TYCppStdLib::stringEx::IsNumeric()
113 {
114     if(""==m_strEx)
115         return false;
116     char chTemp;
117     for(unsigned int i=0; i < m_strEx.length(); i++ )    
118     {
119         strcpy(&chTemp,m_strEx.substr(i,1).c_str());
120         if(!IsDigitInline(&chTemp))
121             return false;
122     }
123     return true;
124 }
125 
126 bool TYCppStdLib::stringEx::IsNumeric(string stString)
127 {
128     if(""==stString)
129         return false;
130     char chTemp;
131     for(unsigned int i=0; i < stString.length(); i++ )    
132     {
133         strcpy(&chTemp,stString.substr(i,1).c_str());
134         if(!IsDigitInline(&chTemp))
135             return false;
136     }
137     return true;
138 }
139 
140 bool TYCppStdLib::stringEx::IsNumeric(string *stString)
141 {
142     if(""==(*stString))
143         return false;
144     char chTemp;
145     for(unsigned int i=0; i < stString->length(); i++ )    
146     {
147         strcpy(&chTemp,stString->substr(i,1).c_str());
148         if(!IsDigitInline(&chTemp))
149             return false;
150     }
151     return true;
152 }
153 
154 /* bool TYCppStdLib::stringEx::IsNumeric(string &stString)
155 {
156     if(""==stString)
157         return false;
158     char chTemp;
159     for(unsigned int i=0; i < stString.length(); i++ )    
160     {
161         strcpy(&chTemp,stString.substr(i,1).c_str());
162         if(!IsDigitInline(&chTemp))
163             return false;
164     }
165     return true;
166 } */
167 
168 //字符串中所有字母转为大写形式
169 string TYCppStdLib::stringEx::Upcase(bool UpMe)
170 {
171     string stTemp = "";                
172     char chTemp;                
173     for(unsigned int i=0; i<m_strEx.size(); i++)    
174     {
175         strcpy(&chTemp,m_strEx.substr(i,1).c_str());
176         stTemp += CharToUpperInline(chTemp);   
177     }
178     if(true==UpMe)
179     {
180         m_strEx=stTemp;
181         Synchronous(false);
182     }
183     return stTemp;
184 }
185 
186 string TYCppStdLib::stringEx::Upcase(string stString)
187 {
188     string stTemp = "";                
189     char chTemp;                
190     for(unsigned int i=0; i<stString.size(); i++)    
191     {
192         strcpy(&chTemp,stString.substr(i,1).c_str());
193         stTemp += CharToUpperInline(chTemp);   
194     }
195     return stTemp;
196 }
197 
198 void TYCppStdLib::stringEx::Upcase(string *stString)
199 {
200     string stTemp = "";                
201     char chTemp;                
202     for(unsigned int i=0; i<stString->size(); i++)    
203     {
204         strcpy(&chTemp,stString->substr(i,1).c_str());
205         stTemp += CharToUpperInline(chTemp);   
206     }
207     *stString=stTemp;
208 }
209 
210 /* void TYCppStdLib::stringEx::Upcase(string &stString)
211 {
212     string stTemp = "";                
213     char chTemp;                
214     for(unsigned int i=0; i<stString.size(); i++)    
215     {
216         strcpy(&chTemp,stString.substr(i,1).c_str());
217         stTemp += CharToUpperInline(chTemp);   
218     }
219     stString=stTemp;
220 } */
221 
222 //字符串中所有字母转为小写形式
223 string TYCppStdLib::stringEx::Lowercase(bool LowMe)
224 {
225     string stTemp = "";                
226     char chTemp;                
227     for(unsigned int i=0; i<m_strEx.size(); i++)    
228     {
229         strcpy(&chTemp,m_strEx.substr(i,1).c_str());
230         stTemp += CharToLowerInline(chTemp);   
231     }
232     if(true==LowMe)
233     {
234         m_strEx=stTemp;
235         Synchronous(false);
236     }
237     return stTemp;
238 }
239 
240 string TYCppStdLib::stringEx::Lowercase(string stString)
241 {
242     string stTemp = "";                
243     char chTemp;                
244     for(unsigned int i=0; i<stString.size(); i++)    
245     {
246         strcpy(&chTemp,stString.substr(i,1).c_str());
247         stTemp += CharToLowerInline(chTemp);   
248     }
249     return stTemp;
250 }
251 
252 void TYCppStdLib::stringEx::Lowercase(string *stString)
253 {
254     string stTemp = "";                
255     char chTemp;                
256     for(unsigned int i=0; i<stString->size(); i++)    
257     {
258         strcpy(&chTemp,stString->substr(i,1).c_str());
259         stTemp += CharToLowerInline(chTemp);   
260     }
261     *stString=stTemp;
262 }
263 
264 /* void TYCppStdLib::stringEx::Lowercase(string &stString)
265 {
266     string stTemp = "";                
267     char chTemp;                
268     for(unsigned int i=0; i<stString.size(); i++)    
269     {
270         strcpy(&chTemp,stString.substr(i,1).c_str());
271         stTemp += CharToLowerInline(chTemp);   
272     }
273     stString=stTemp;
274 } */
275 
276 //去掉左边指定字符串
277 string TYCppStdLib::stringEx::LTrim(bool LTrMe, string LTrStr)
278 {
279     string stTemp=m_strEx;
280     if (!stTemp.length()) 
281         return stTemp;
282     int i=0;    
283     string buf;
284     while (stTemp.substr(i,1) == LTrStr)
285         i++;
286     if(0==i)
287         return stTemp;
288     if (i == stTemp.length())
289     {
290         stTemp = "";
291         return stTemp;
292     }
293     stTemp = stTemp.substr(i,stTemp.length()-i);
294     if(true==LTrMe)
295     {
296         m_strEx=stTemp;
297         Synchronous(false);
298     }
299     return stTemp;
300 }
301 
302 string TYCppStdLib::stringEx::LTrim(string stString, string LTrStr)
303 {
304     string stTemp=stString;
305     if (!stTemp.length()) 
306         return stTemp;
307     int i=0;    
308     string buf;
309     while (stTemp.substr(i,1) == LTrStr)
310         i++;
311     if(0==i)
312         return stTemp;
313     if (i == stTemp.length())
314     {
315         stTemp = "";
316         return stTemp;
317     }
318     stTemp = stTemp.substr(i,stTemp.length()-i);
319     return stTemp;
320 }
321 
322 void TYCppStdLib::stringEx::LTrim(string *stString, string LTrStr)
323 {
324     if (!stString->length()) 
325         return;
326     int i=0;    
327     string buf;
328     while (stString->substr(i,1) == LTrStr)
329         i++;
330     if(0==i)
331         return;
332     if (i == stString->length())
333     {
334         *stString = "";
335         return;
336     }
337     *stString = stString->substr(i,stString->length()-i);
338 }
339 
340 /* void TYCppStdLib::stringEx::LTrim(string &stString, string LTrStr)
341 {
342     if (!stString.length()) 
343         return;
344     int i=0;    
345     string buf;
346     while (stString.substr(i,1) == LTrStr)
347         i++;
348     if(0==i)
349         return;
350     if (i == stString.length())
351     {
352         stString = "";
353         return;
354     }
355     stString = stString.substr(i,stString.length()-i);
356 } */
357 
358 //去掉右边指定字符串
359 string TYCppStdLib::stringEx::RTrim(bool RTrMe, string RTrStr)
360 {
361     string stTemp=m_strEx;
362     if (!stTemp.length())
363         return stTemp;
364     int i;
365     i = stTemp.length()-1;
366     while (stTemp.substr(i,1) == RTrStr)
367     {
368         stTemp = stTemp.substr(0, i);    
369         i--;
370     }
371     if(true==RTrMe)
372     {
373         m_strEx=stTemp;
374         Synchronous(false);
375     }
376     return stTemp;
377 }
378 
379 string TYCppStdLib::stringEx::RTrim(string stString, string RTrStr)
380 {
381     string stTemp=stString;
382     if (!stTemp.length())
383         return stTemp;
384     int i;
385     i = stTemp.length()-1;
386     while (stTemp.substr(i,1) == RTrStr)
387     {
388         stTemp = stTemp.substr(0, i);    
389         i--;
390     }
391     return stTemp;
392 }
393 
394 void TYCppStdLib::stringEx::RTrim(string *stString, string RTrStr)
395 {
396     if (!stString->length())
397         return;
398     int i;
399     i = stString->length()-1;
400     while (stString->substr(i,1) == RTrStr)
401     {
402         *stString = stString->substr(0, i);    
403         i--;
404     }
405 }
406 
407 /* void TYCppStdLib::stringEx::RTrim(string &stString, string RTrStr)
408 {
409     if (!stString.length())
410         return;
411     int i;
412     i = stString.length()-1;
413     while (stString.substr(i,1) == RTrStr)
414     {
415         stString = stString.substr(0, i);    
416         i--;
417     }
418 } */
419 
420 //去掉两边指定字符串
421 string TYCppStdLib::stringEx::Trim(bool TrMe, string TrStr)
422 {
423     string stTemp=m_strEx;
424     stTemp=RTrim(stTemp, TrStr);
425     if (stTemp.length())
426     stTemp=LTrim(stTemp, TrStr);
427     if(true==TrMe)
428     {
429         m_strEx=stTemp;
430         Synchronous(false);
431     }
432     return stTemp;
433 }
434 
435 string TYCppStdLib::stringEx::Trim(string stString, string TrStr)
436 {
437     string stTemp=stString;
438     stTemp=RTrim(stTemp, TrStr);
439     if (stTemp.length())
440     stTemp=LTrim(stTemp, TrStr);
441     return stTemp;
442 }
443 
444 void TYCppStdLib::stringEx::Trim(string *stString, string TrStr)
445 {
446     RTrim(stString, TrStr);
447     if (stString->length())
448     LTrim(stString, TrStr);
449 }
450 
451 /* void TYCppStdLib::stringEx::Trim(string &stString, string TrStr)
452 {
453     RTrim(stString, TrStr);
454     if (stString.length())
455         LTrim(stString, TrStr);
456 } */

 

posted @ 2012-05-08 13:53  天远  阅读(747)  评论(0)    收藏  举报

版权所有 © 2010-2020 YuanPeirong TianYuan All Rights Reserved. Powered By 天远