# 字串處理 String,char ###### tags: `C++Book` ## 字串轉字元陣列 s.c_str() 功能:將String物件轉換成C語言形式的字串常數 ## char[] ### <cype.h> #### 以下為字元測試的函數 | 函數名稱 | 功能 | 函數原型 | | --- | --- | --- | | isdigit | 測試字元是否為數字 | [int isdigit(int);](http://pydoing.blogspot.com/2010/07/c-isdigit.html) | | isalpha | 測試字元是否為字母 | [int isalpha(int);](http://pydoing.blogspot.com/2010/07/c-isalpha.html) | | isalnum | 測試字元是否為數字或字母 | [int isalnum(int);](http://pydoing.blogspot.com/2010/07/c-isalnum.html) | | isxdigit | 測試字元是否為十六進位數字 | [int isxdigit(int);](http://pydoing.blogspot.com/2010/07/c-isxdigit.html) | | islower | 測試字元是否為小寫字母 | [int islower(int);](http://pydoing.blogspot.com/2010/07/c-islower.html) | | isupper | 測試字元是否為大寫字母 | [int isupper(int);](http://pydoing.blogspot.com/2010/07/c-isupper.html) | | isascii | 測試字元是否為 ASCII | [int isascii(int);](http://pydoing.blogspot.com/2010/07/c-isascii.html) | | isblank | 測試是否為空白字元 | [int isblank(int);](http://pydoing.blogspot.com/2010/07/c-isblank.html) | | isspace | 測試字元是否為空格 | [int isspace(int);](http://pydoing.blogspot.com/2010/07/c-isspace.html) | | iscntrl | 測試是否為控制字元 | [int iscntrl(int);](http://pydoing.blogspot.com/2010/07/c-iscntrl.html) | | ispunct | 測試是否為空格、數字、字母以外的可列印字元 | [int ispunct(int);](http://pydoing.blogspot.com/2010/07/c-ispunct.html) | | isprint | 測試是否為含括空格以內的可列印字元 | [int isprint(int);](http://pydoing.blogspot.com/2010/07/c-isprint.html) | | isgraph | 測試是否為空格以外的可列印字元 | [int isgraph(int);](http://pydoing.blogspot.com/2010/07/c-isgraph.html) | #### 以下為英文字母大、小寫轉換的函數 | 函數名稱 | 功能 | 函數原型 | | --- | --- | --- | | tolower | 將大寫字母轉換為小寫 | [int tolower(int);](http://pydoing.blogspot.com/2010/07/c-tolower.html) | | toupper | 將小寫字母轉換為大寫 | [int toupper(int);](http://pydoing.blogspot.com/2010/07/c-toupper.html) | ### 字串轉數字 ```cpp char myString [] = "1111"; // 宣告字串 (字元陣列) int a = atoi ( myString ); // 將字串轉整數 printf (a + 2222); // 輸出:3333 ``` ### memset 用來對一段內存空間全部設置為某個字符,一般用在對定義的字符串進行初始化為『 '或『\0';例: ```cpp char a[100];memset(a, '\0', sizeof(a));  ``` ### strcpy  原型:extern char \*strcpy(char \*dest,char \*src);  用法:#i nclude  功能:把src所指由NULL結束的字符串複製到dest所指的數組中。 說明:src和dest所指內存區域不可以重疊且dest必須有足夠的空間來容納src的字符串。 返回指向dest的指針。 ### memcpy  原型:extern void \*memcpy(void \*dest, void \*src, unsigned int count); 用法:#i nclude  功能:由src所指內存區域複製count個字節到dest所指內存區域。 說明:src和dest所指內存區域不能重疊,函數返回指向dest的指針。