?? c++常用字符串處理函數(shù)及使用示例.txt
字號(hào):
本文關(guān)鍵詞: c++ 字符串 函數(shù)
char *strcpy(char *s1, const char *s2)
將字符串s2復(fù)制到字符串?dāng)?shù)組s1中,返回s1的值
char *strncpy(char *s1, const char *s2, size_t n)
將字符串s2中最多n個(gè)字符復(fù)制到字符串?dāng)?shù)組s1中,返回s1的值
char *strcat(char *s1, const char *s2)
將字符串s2添加到字符串s1的后面。s2的第一個(gè)字符重定義s1的null終止符。返回s1的值
char *strncat(char *s1, const char *s2, size_t n)
將字符串s2中最多n個(gè)字符添加到字符串s1的后面。s2的第一個(gè)字符重定義s1的null終止符。返回s1的值
int strcmp(const char *s1, const char *s2)
比較字符串s1和字符串s2。函數(shù)在s1等于、小于或大于s2時(shí)分別返回0、小于0或者大于0的值
int strncmp(const char *s1, const char *s2, size_t n)
比較字符串s1中的n個(gè)字符和字符串s2。函數(shù)在s1等于、小于或大于s2時(shí)分別返回0、小于0或者大于0的值
char * strtok(char *s1,const char *s2)
用一系列strtok調(diào)用將s1字符串標(biāo)記化(將字符串分成各個(gè)邏輯組件,如同一行文本中的每個(gè)單詞),用字符串s2所包含的字符分隔。 首次調(diào)用時(shí)包含s1為第一個(gè)參數(shù),后面調(diào)用時(shí)繼續(xù)標(biāo)記化同一字符串,包含NULL為第一個(gè)參數(shù)。每次調(diào)用時(shí)返回當(dāng)前標(biāo)記指針。如果函數(shù)調(diào)用時(shí)不再有更多標(biāo)記,則返回NULL
size_t strlen(const char *s)
確定字符串長(zhǎng)度,返回null終止符之前的字符數(shù)
使用示例:
//源代碼在Visual c++6.0環(huán)境下編譯通過
#include <iostream.h>
#include <string.h>
int main()
{
char str1[50] = "Happy birthday to ", str2[] = "coffeehu";
char temp1[100],temp2[6], * temp;
char str[] = "This is a sentence with 7 tokens";
strcpy(temp1, str1);
strncpy(temp2, str1, 5);
temp2[5] = '';
cout << "strcpy result: " <<temp1 << "n";
cout << "strncpy result: " << temp2 << "n";
cout << "strcat result: " << strcat(str1, str2) << "n";
cout << "strncat result: " << strncat(str1, str2, 6) <<"n";
cout << "strcmp result: " << strcmp(temp2,"Happ") <<"n";
cout << "strncmp result: " << strncmp(str1,"Happy",5) <<"n";
//strtok function eg.
temp = strtok(str, " ");
while(temp != NULL)
{
cout << temp <<'n';
temp = strtok(NULL, " ");
}
cout << "strlen result: " << strlen(str2) <<"n";
return 0;
}
1. UpperCase
將指定的AnsiString字符串轉(zhuǎn)換為大寫形式,函數(shù)原型如下:
AnsiString __fastcall UpperCase(const AnsiString S);
2. LowerCase
將指定的AnsiString字符串轉(zhuǎn)換為小寫形式,函數(shù)原型如下:
AnsiString __fastcall LowerCase(const AnsiString S);
3. CompareStr
比較兩個(gè)AnsiString字符串,函數(shù)原型如下:
int __fastcall CompareStr(const AnsiString S1, const AnsiString S2);
4. CompareText
比較兩個(gè)AnsiString字符串,函數(shù)原型如下:
int __fastcall CompareText(const AnsiString S1, const AnsiString S2);
5. StrLen
返回字符串的長(zhǎng)度,函數(shù)原型如下:
Cardinal __fastcall StrLen(const char * Str);
6. StrEnd
返回字符串結(jié)尾指針,函數(shù)原型如下:
char * __fastcall StrEnd(const char * Str);
7. StrMove
從源字符串向目的字符串拷貝指定數(shù)目的字符,函數(shù)原型如下:
char * __fastcall StrMove(char * Dest, const char * Source, Cardinal Count);
8. StrCopy
將源字符串拷貝到目的字符串中,函數(shù)原型如下:
char * __fastcall StrCopy(char * Dest, const char * Source);
9. StrECopy
將源字符串拷貝到目的字符串中,并返回目的字符串結(jié)尾指針,函數(shù)原型如下:
char * __fastcall StrECopy(char * Dest, const char * Source);
10.StrLCopy
將源字符串指定數(shù)目的字符拷貝到目的字符串中,并返回目的字符串指針,函數(shù)原型如下:
char * __fastcall StrLCopy(char * Dest, const char * Source, Cardinal MaxLen);
11.StrPCopy
將AnsiString類型的源字符串拷貝到目的字符串中,并返回目的字符串指針,函數(shù)原型如下:
char * __fastcall StrPCopy(char * Dest, const AnsiString Source);
12.StrPLCopy
將源字符串(AnsiString類型)指定數(shù)目的字符拷貝到目的字符串中,并返回目的字符串
指針,函數(shù)原型如下:
char * __fastcall StrPLCopy(char * Dest, const AnsiString Source, Cardinal MaxLen);
13.StrCat
連接兩個(gè)字符串,并返回目的字符串指針,函數(shù)原型如下:
char * __fastcall StrCat(char * Dest, const char * Source);
14.StrLCat
將指定數(shù)目的源字符串連接到目的字符串,并返回目的字符串指針,函數(shù)原型如下:
char * __fastcall StrLCat(char * Dest, const char * Source, Cardinal MaxLen);
15.StrComp
兩個(gè)字符串相到比較,返回比較的結(jié)果,函數(shù)原型如下:
int __fastcall StrComp(const char * Str1, const char * Str2);
16.StrIComp
兩個(gè)字符串相互比較(不論大小寫),返回比較的結(jié)果,函數(shù)原型如下:
int __fastcall StrIComp(const char * Str1, const char * Str2);
17.StrLComp
對(duì)兩個(gè)字符串指定數(shù)目的字符進(jìn)行比較操作,函數(shù)原型如下:
int __fastcall StrLComp(const char * Str1, const char * Str2, Cardinal MaxLen);
18.StrScan
在指定的字符串中尋找特定的字符,并返回字符串中第一個(gè)特定字符的指針,函數(shù)原型如下:
char * __fastcall StrScan(const char * Str, char Chr);
19.StrRScan
在指定的字符串中尋找特定的字符,并返回字符串中最后一個(gè)特定字符的指針,函數(shù)原型如下:
char * __fastcall StrRScan(const char * Str, char Chr);
20.StrPos
在Strl所指定的字符串中尋找Str2所指定的子字符串,并返回Str2在Str2中第一個(gè)子字符的指針,函數(shù)原型如下:
char * __fastcall StrPos(const char * Str1, const char * Str2);
21.StrUpper
將字符串轉(zhuǎn)換為大寫形式,函數(shù)原型如下:
char * __fastcall StrUpper(char * Str);
22.StrLower
將字符串轉(zhuǎn)換為小寫形式,函數(shù)原型如下:
char * __fastcall StrLower(char * Str);
23.StrPas
將指定的字符串轉(zhuǎn)換為AnsiString類型字符串對(duì)象,函數(shù)原型如下:
AnsiString __fastcall StrPas(const char * Str);
24.StrAlloc
為字符串分配指定字節(jié)的內(nèi)存,并返回內(nèi)存指針,函數(shù)原型如下:
char * __fastcall StrAlloc(Cardinal Size);
25.StrBufSize
返回*Str所指向內(nèi)存的大小,函數(shù)原型如下:
Cardinal __fastcall StrBufSize(const char * Str);
26.StrNew
在堆中為指定字符串分配空間,并將字符串拷貝到此空間中,函數(shù)原型如下:
char * __fastcall StrNew(const char * Str);
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -