手机版
你好,游客 登录 注册
背景:
阅读新闻

C++标准库string类型

[日期:2012-10-05] 来源:Linux社区  作者:zhy_cheng [字体:大 中 小]

4.字符串对象中的字符处理

我们经常要对string对象中的单个字符进行处理,例如,通常要知道某个特殊字符是否为空白字符,字母或数字。在cctype头文件中定义了对string的操作。下面是ctype.h头文件

  1. /*** 
  2. *ctype.h - character conversion macros and ctype macros 
  3. * 
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved. 
  5. * 
  6. *Purpose: 
  7. *       Defines macros for character classification/conversion. 
  8. *       [ANSI/System V] 
  9. * 
  10. *       [Public] 
  11. * 
  12. ****/ 
  13.  
  14. #if     _MSC_VER > 1000  
  15. #pragma once  
  16. #endif  
  17.  
  18. #ifndef _INC_CTYPE  
  19. #define _INC_CTYPE  
  20.  
  21. #if     !defined(_WIN32) && !defined(_MAC)  
  22. #error ERROR: Only Mac or Win32 targets supported!  
  23. #endif  
  24.  
  25.  
  26. #ifdef  __cplusplus  
  27. extern "C" { 
  28. #endif  
  29.  
  30.  
  31.  
  32. /* Define _CRTIMP */ 
  33.  
  34. #ifndef _CRTIMP  
  35. #ifdef  _DLL  
  36. #define _CRTIMP __declspec(dllimport)  
  37. #else   /* ndef _DLL */  
  38. #define _CRTIMP  
  39. #endif  /* _DLL */  
  40. #endif  /* _CRTIMP */  
  41.  
  42.  
  43. /* Define __cdecl for non-Microsoft compilers */ 
  44.  
  45. #if     ( !defined(_MSC_VER) && !defined(__cdecl) )  
  46. #define __cdecl  
  47. #endif  
  48.  
  49. /* Define _CRTAPI1 (for compatibility with the NT SDK) */ 
  50.  
  51. #ifndef _CRTAPI1  
  52. #if _MSC_VER >= 800 && _M_IX86 >= 300  
  53. #define _CRTAPI1 __cdecl  
  54. #else  
  55. #define _CRTAPI1  
  56. #endif  
  57. #endif  
  58.  
  59. #ifndef _WCHAR_T_DEFINED  
  60. typedef unsigned short wchar_t; 
  61. #define _WCHAR_T_DEFINED  
  62. #endif  
  63.  
  64. #ifndef _MAC  
  65. #ifndef _WCTYPE_T_DEFINED  
  66. typedef wchar_t wint_t; 
  67. typedef wchar_t wctype_t; 
  68. #define _WCTYPE_T_DEFINED  
  69. #endif  
  70.  
  71. #ifndef WEOF  
  72. #define WEOF (wint_t)(0xFFFF)  
  73. #endif  
  74. #endif  /* ndef _MAC */  
  75.  
  76.  
  77. #ifndef _CTYPE_DISABLE_MACROS  
  78. _CRTIMP extern unsigned short _ctype[]; 
  79. _CRTIMP extern unsigned short *_pctype; 
  80. #ifndef _MAC  
  81. _CRTIMP extern wctype_t *_pwctype; 
  82. #endif  /* ndef _MAC */  
  83. #endif  /* _CTYPE_DISABLE_MACROS */  
  84.  
  85.  
  86. /* set bit masks for the possible character types */ 
  87.  
  88. #define _UPPER          0x1     /* upper case letter */  
  89. #define _LOWER          0x2     /* lower case letter */  
  90. #define _DIGIT          0x4     /* digit[0-9] */  
  91. #define _SPACE          0x8     /* tab, carriage return, newline, */  
  92.                                 /* vertical tab or form feed */ 
  93. #define _PUNCT          0x10    /* punctuation character */  
  94. #define _CONTROL        0x20    /* control character */  
  95. #define _BLANK          0x40    /* space char */  
  96. #define _HEX            0x80    /* hexadecimal digit */  
  97.  
  98. #define _LEADBYTE       0x8000                  /* multibyte leadbyte */  
  99. #define _ALPHA          (0x0100|_UPPER|_LOWER)  /* alphabetic character */  
  100.  
  101.  
  102. /* character classification function prototypes */ 
  103.  
  104. #ifndef _CTYPE_DEFINED  
  105.  
  106. _CRTIMP int __cdecl _isctype(int, int); 
  107. _CRTIMP int __cdecl isalpha(int); 
  108. _CRTIMP int __cdecl isupper(int); 
  109. _CRTIMP int __cdecl islower(int); 
  110. _CRTIMP int __cdecl isdigit(int); 
  111. _CRTIMP int __cdecl isxdigit(int); 
  112. _CRTIMP int __cdecl isspace(int); 
  113. _CRTIMP int __cdecl ispunct(int); 
  114. _CRTIMP int __cdecl isalnum(int); 
  115. _CRTIMP int __cdecl isprint(int); 
  116. _CRTIMP int __cdecl isgraph(int); 
  117. _CRTIMP int __cdecl iscntrl(int); 
  118. _CRTIMP int __cdecl toupper(int); 
  119. _CRTIMP int __cdecl tolower(int); 
  120. _CRTIMP int __cdecl _tolower(int); 
  121. _CRTIMP int __cdecl _toupper(int); 
  122. _CRTIMP int __cdecl __isascii(int); 
  123. _CRTIMP int __cdecl __toascii(int); 
  124. _CRTIMP int __cdecl __iscsymf(int); 
  125. _CRTIMP int __cdecl __iscsym(int); 
  126. #define _CTYPE_DEFINED  
  127. #endif  
  128.  
  129.  
  130. #ifndef _MAC  
  131. #ifndef _WCTYPE_DEFINED  
  132.  
  133. /* wide function prototypes, also declared in wchar.h  */ 
  134.  
  135. /* character classification function prototypes */ 
  136.  
  137. _CRTIMP int __cdecl iswalpha(wint_t); 
  138. _CRTIMP int __cdecl iswupper(wint_t); 
  139. _CRTIMP int __cdecl iswlower(wint_t); 
  140. _CRTIMP int __cdecl iswdigit(wint_t); 
  141. _CRTIMP int __cdecl iswxdigit(wint_t); 
  142. _CRTIMP int __cdecl iswspace(wint_t); 
  143. _CRTIMP int __cdecl iswpunct(wint_t); 
  144. _CRTIMP int __cdecl iswalnum(wint_t); 
  145. _CRTIMP int __cdecl iswprint(wint_t); 
  146. _CRTIMP int __cdecl iswgraph(wint_t); 
  147. _CRTIMP int __cdecl iswcntrl(wint_t); 
  148. _CRTIMP int __cdecl iswascii(wint_t); 
  149. _CRTIMP int __cdecl isleadbyte(int); 
  150.  
  151. _CRTIMP wchar_t __cdecl towupper(wchar_t); 
  152. _CRTIMP wchar_t __cdecl towlower(wchar_t); 
  153.  
  154. _CRTIMP int __cdecl iswctype(wint_t, wctype_t); 
  155.  
  156. /* --------- The following functions are OBSOLETE --------- */ 
  157. _CRTIMP int __cdecl is_wctype(wint_t, wctype_t); 
  158. /*  --------- The preceding functions are OBSOLETE --------- */ 
  159.  
  160. #define _WCTYPE_DEFINED  
  161. #endif  
  162. #endif  /* ndef _MAC */  
  163.  
  164.  
  165. /* the character classification macro definitions */ 
  166.  
  167. #ifndef _CTYPE_DISABLE_MACROS  
  168.  
  169. /* 
  170.  * Maximum number of bytes in multi-byte character in the current locale 
  171.  * (also defined in stdlib.h). 
  172.  */ 
  173. #ifndef MB_CUR_MAX  
  174.  
  175. #define MB_CUR_MAX __mb_cur_max  
  176. _CRTIMP extern int __mb_cur_max; 
  177.  
  178. #endif  /* MB_CUR_MAX */  
  179.  
  180. #ifdef  _MAC  
  181.  
  182. #ifndef __cplusplus  
  183. #define isalpha(_c)     ( _pctype[_c] & (_UPPER|_LOWER) )  
  184. #define isupper(_c)     ( _pctype[_c] & _UPPER )  
  185. #define islower(_c)     ( _pctype[_c] & _LOWER )  
  186. #define isdigit(_c)     ( _pctype[_c] & _DIGIT )  
  187. #define isxdigit(_c)    ( _pctype[_c] & _HEX )  
  188. #define isspace(_c)     ( _pctype[_c] & _SPACE )  
  189. #define ispunct(_c)     ( _pctype[_c] & _PUNCT )  
  190. #define isalnum(_c)     ( _pctype[_c] & (_UPPER|_LOWER|_DIGIT) )  
  191. #define isprint(_c)     ( _pctype[_c] & (_BLANK|_PUNCT|_UPPER|_LOWER|_DIGIT) )  
  192. #define isgraph(_c)     ( _pctype[_c] & (_PUNCT|_UPPER|_LOWER|_DIGIT) )  
  193. #define iscntrl(_c)     ( _pctype[_c] & _CONTROL )  
  194. #elif 0         /* Pending ANSI C++ integration */  
  195. inline int isalpha(int _C)      {return (_pctype[_C] & (_UPPER|_LOWER)); } 
  196. inline int isupper(int _C)      {return (_pctype[_C] & _UPPER); } 
  197. inline int islower(int _C)      {return (_pctype[_C] & _LOWER); } 
  198. inline int isdigit(int _C)      {return (_pctype[_C] & _DIGIT); } 
  199. inline int isxdigit(int _C)     {return (_pctype[_C] & _HEX); } 
  200. inline int isspace(int _C)      {return (_pctype[_C] & _SPACE); } 
  201. inline int ispunct(int _C)      {return (_pctype[_C] & _PUNCT); } 
  202. inline int isalnum(int _C)      {return (_pctype[_C] & (_UPPER|_LOWER|_DIGIT)); } 
  203. inline int isprint(int _C) 
  204.         {return (_pctype[_C] & (_BLANK|_PUNCT|_UPPER|_LOWER|_DIGIT)); } 
  205. inline int isgraph(int _C) 
  206.         {return (_pctype[_C] & (_PUNCT|_UPPER|_LOWER|_DIGIT)); } 
  207. inline int iscntrl(int _C)      {return (_pctype[_C] & _CONTROL); } 
  208. #endif  /* __cplusplus */  
  209.  
  210. #else   /* ndef _MAC */  
  211.  
  212. #ifndef __cplusplus  
  213. #define isalpha(_c)     (MB_CUR_MAX > 1 ? _isctype(_c,_ALPHA) : _pctype[_c] & _ALPHA)  
  214. #define isupper(_c)     (MB_CUR_MAX > 1 ? _isctype(_c,_UPPER) : _pctype[_c] & _UPPER)  
  215. #define islower(_c)     (MB_CUR_MAX > 1 ? _isctype(_c,_LOWER) : _pctype[_c] & _LOWER)  
  216. #define isdigit(_c)     (MB_CUR_MAX > 1 ? _isctype(_c,_DIGIT) : _pctype[_c] & _DIGIT)  
  217. #define isxdigit(_c)    (MB_CUR_MAX > 1 ? _isctype(_c,_HEX)   : _pctype[_c] & _HEX)  
  218. #define isspace(_c)     (MB_CUR_MAX > 1 ? _isctype(_c,_SPACE) : _pctype[_c] & _SPACE)  
  219. #define ispunct(_c)     (MB_CUR_MAX > 1 ? _isctype(_c,_PUNCT) : _pctype[_c] & _PUNCT)  
  220. #define isalnum(_c)     (MB_CUR_MAX > 1 ? _isctype(_c,_ALPHA|_DIGIT) : _pctype[_c] & (_ALPHA|_DIGIT))  
  221. #define isprint(_c)     (MB_CUR_MAX > 1 ? _isctype(_c,_BLANK|_PUNCT|_ALPHA|_DIGIT) : _pctype[_c] & (_BLANK|_PUNCT|_ALPHA|_DIGIT))  
  222. #define isgraph(_c)     (MB_CUR_MAX > 1 ? _isctype(_c,_PUNCT|_ALPHA|_DIGIT) : _pctype[_c] & (_PUNCT|_ALPHA|_DIGIT))  
  223. #define iscntrl(_c)     (MB_CUR_MAX > 1 ? _isctype(_c,_CONTROL) : _pctype[_c] & _CONTROL)  
  224. #elif   0         /* Pending ANSI C++ integration */  
  225. inline int isalpha(int _C) 
  226.         {return (MB_CUR_MAX > 1 ? _isctype(_C,_ALPHA) : _pctype[_C] & _ALPHA); } 
  227. inline int isupper(int _C) 
  228.         {return (MB_CUR_MAX > 1 ? _isctype(_C,_UPPER) : _pctype[_C] & _UPPER); } 
  229. inline int islower(int _C) 
  230.         {return (MB_CUR_MAX > 1 ? _isctype(_C,_LOWER) : _pctype[_C] & _LOWER); } 
  231. inline int isdigit(int _C) 
  232.         {return (MB_CUR_MAX > 1 ? _isctype(_C,_DIGIT) : _pctype[_C] & _DIGIT); } 
  233. inline int isxdigit(int _C) 
  234.         {return (MB_CUR_MAX > 1 ? _isctype(_C,_HEX)   : _pctype[_C] & _HEX); } 
  235. inline int isspace(int _C) 
  236.         {return (MB_CUR_MAX > 1 ? _isctype(_C,_SPACE) : _pctype[_C] & _SPACE); } 
  237. inline int ispunct(int _C) 
  238.         {return (MB_CUR_MAX > 1 ? _isctype(_C,_PUNCT) : _pctype[_C] & _PUNCT); } 
  239. inline int isalnum(int _C) 
  240.         {return (MB_CUR_MAX > 1 ? _isctype(_C,_ALPHA|_DIGIT) 
  241.                 : _pctype[_C] & (_ALPHA|_DIGIT)); } 
  242. inline int isprint(int _C) 
  243.         {return (MB_CUR_MAX > 1 ? _isctype(_C,_BLANK|_PUNCT|_ALPHA|_DIGIT) 
  244.                 : _pctype[_C] & (_BLANK|_PUNCT|_ALPHA|_DIGIT)); } 
  245. inline int isgraph(int _C) 
  246.         {return (MB_CUR_MAX > 1 ? _isctype(_C,_PUNCT|_ALPHA|_DIGIT) 
  247.                 : _pctype[_C] & (_PUNCT|_ALPHA|_DIGIT)); } 
  248. inline int iscntrl(int _C) 
  249.         {return (MB_CUR_MAX > 1 ? _isctype(_C,_CONTROL) 
  250.                 : _pctype[_C] & _CONTROL); } 
  251. #endif  /* __cplusplus */  
  252.  
  253. #endif  /* _MAC */  
  254.  
  255. #define _tolower(_c)    ( (_c)-'A'+'a' )  
  256. #define _toupper(_c)    ( (_c)-'a'+'A' )  
  257.  
  258. #define __isascii(_c)   ( (unsigned)(_c) < 0x80 )  
  259. #define __toascii(_c)   ( (_c) & 0x7f )  
  260.  
  261. #ifndef _WCTYPE_INLINE_DEFINED  
  262. #ifndef __cplusplus  
  263. #define iswalpha(_c)    ( iswctype(_c,_ALPHA) )  
  264. #define iswupper(_c)    ( iswctype(_c,_UPPER) )  
  265. #define iswlower(_c)    ( iswctype(_c,_LOWER) )  
  266. #define iswdigit(_c)    ( iswctype(_c,_DIGIT) )  
  267. #define iswxdigit(_c)   ( iswctype(_c,_HEX) )  
  268. #define iswspace(_c)    ( iswctype(_c,_SPACE) )  
  269. #define iswpunct(_c)    ( iswctype(_c,_PUNCT) )  
  270. #define iswalnum(_c)    ( iswctype(_c,_ALPHA|_DIGIT) )  
  271. #define iswprint(_c)    ( iswctype(_c,_BLANK|_PUNCT|_ALPHA|_DIGIT) )  
  272. #define iswgraph(_c)    ( iswctype(_c,_PUNCT|_ALPHA|_DIGIT) )  
  273. #define iswcntrl(_c)    ( iswctype(_c,_CONTROL) )  
  274. #define iswascii(_c)    ( (unsigned)(_c) < 0x80 )  
  275. #define isleadbyte(_c)  (_pctype[(unsigned char)(_c)] & _LEADBYTE)  
  276. #elif   0         /* __cplusplus */  
  277. inline int iswalpha(wint_t _C) {return (iswctype(_C,_ALPHA)); } 
  278. inline int iswupper(wint_t _C) {return (iswctype(_C,_UPPER)); } 
  279. inline int iswlower(wint_t _C) {return (iswctype(_C,_LOWER)); } 
  280. inline int iswdigit(wint_t _C) {return (iswctype(_C,_DIGIT)); } 
  281. inline int iswxdigit(wint_t _C) {return (iswctype(_C,_HEX)); } 
  282. inline int iswspace(wint_t _C) {return (iswctype(_C,_SPACE)); } 
  283. inline int iswpunct(wint_t _C) {return (iswctype(_C,_PUNCT)); } 
  284. inline int iswalnum(wint_t _C) {return (iswctype(_C,_ALPHA|_DIGIT)); } 
  285. inline int iswprint(wint_t _C) 
  286.         {return (iswctype(_C,_BLANK|_PUNCT|_ALPHA|_DIGIT)); } 
  287. inline int iswgraph(wint_t _C) 
  288.         {return (iswctype(_C,_PUNCT|_ALPHA|_DIGIT)); } 
  289. inline int iswcntrl(wint_t _C) {return (iswctype(_C,_CONTROL)); } 
  290. inline int iswascii(wint_t _C) {return ((unsigned)(_C) < 0x80); } 
  291.  
  292. inline int isleadbyte(int _C) 
  293.         {return (_pctype[(unsigned char)(_C)] & _LEADBYTE); } 
  294. #endif  /* __cplusplus */  
  295. #define _WCTYPE_INLINE_DEFINED  
  296. #endif  /* _WCTYPE_INLINE_DEFINED */  
  297.  
  298. /* MS C version 2.0 extended ctype macros */ 
  299.  
  300. #define __iscsymf(_c)   (isalpha(_c) || ((_c) == '_'))  
  301. #define __iscsym(_c)    (isalnum(_c) || ((_c) == '_'))  
  302.  
  303. #endif  /* _CTYPE_DISABLE_MACROS */  
  304.  
  305.  
  306. #if     !__STDC__  
  307.  
  308. /* Non-ANSI names for compatibility */ 
  309.  
  310. #ifndef _CTYPE_DEFINED  
  311. _CRTIMP int __cdecl isascii(int); 
  312. _CRTIMP int __cdecl toascii(int); 
  313. _CRTIMP int __cdecl iscsymf(int); 
  314. _CRTIMP int __cdecl iscsym(int); 
  315. #else  
  316. #define isascii __isascii  
  317. #define toascii __toascii  
  318. #define iscsymf __iscsymf  
  319. #define iscsym  __iscsym  
  320. #endif  
  321.  
  322. #endif  /* __STDC__ */  
  323.  
  324. #ifdef  __cplusplus  
  325. } 
  326. #endif  
  327.  
  328.  
  329. #endif  /* _INC_CTYPE */ 

其中的函数的解释如下

  1. isalnum(c)            如果c是数字或者字母,则为true 
  2. isalpha(c)            如果c是字母,则为true 
  3. iscntrl(c)            如果c是控制字符,则为true 
  4. isdigit(c)            如果c是数字,则为true 
  5. isgraph(c)            如果c不是空格,但可打印,则为true 
  6. islower(c)            如果c是小写字母,则为true 
  7. isprint(c)            如果c是可以打印的字符,这位true 
  8. ispunct(c)            如果c是标点符号,则为true 
  9. isspace(c)            如果c是空白字符,则为true 
  10. isupper(c)            如果c是大写字母,则为true 
  11. isxdigit(c)           如果c是十六进制数,则为true 
  12. tolower(c)            如果c是大写字母与,则返回其小写字母形式,否则直接返回c 
  13. toupper(c)            如果c是小写字母,则返回其大写字母形式,否则直接返回c 

大部分函数式测试一个给定的字符是否符合条件,并返回一个int值作为真值。如果测试失败则返回0,否则返回一个无意义的非0值,表示被测试的字符符合条件。

 

 

 

linux
相关资讯       C++  String 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款