C 字符串
声明
C 语言没有单独的字符串类型,字符串被当作字符数组,即char[]
.
字符串变量可以声明成一个字符数组,也可以声明成一个指针,指向一个字符数组。
所有字符串都以\0
结束。
1 2 3 4 5 6 7
| char s1[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; char s2[6] = "Hello"; char s3[] = "Hello";
char* s4 = "Hello";
|
若字符数组长度大于字符串长度,则会用\0
填充剩余的空间。
C 语言允许合并多个字符串字面量,如果这些字符串之间只有空格或换行,C 语言会将它们自动合并。
1 2 3 4
| char greeting[50] = "Hello, " "how are you " "today!";
char greeting[50] = "Hello, how are you today!";
|
对于已经定义的字符数组,我们不能直接使用赋值运算符进行整体赋值,而是必须使用 C 语言提供的strcpy()
函数。
1 2 3
| char str[10]; str = "Hello"; strcpy(str, "Hello");
|
若使用指针变量,则可以直接指向字符串常量或字符数组:
1 2 3 4 5 6 7
| char* p; p = "World"; *(p+3) = 'z';
char str[] = "Hello, world!"; p = str; *(p+3) = 'z';
|
strlen() 函数
strlen()
函数返回字符串的字节长度,不包括末尾的空字符\0
.
1 2
| size_t strlen(const char* s);
|
它的参数是字符串变量,返回的是size_t
类型的无符号整数,除非是极长的字符串,一般情况下当作int
处理即可。
注意,字符串长度strlen()
与字符数组长度sizeof()
是两个不同的概念。
1 2 3 4
| char s[50] = "hello\0world12"; cout << strlen(s) << endl; cout << strlen(s+6) << endl; cout << sizeof(s) << endl;
|
strcpy() 函数
strcpy()
函数用于将一个字符串复制到一个字符数组中。
1 2
| char* strcpy(char dest[], const char src[]);
|
strcpy()
的返回值是一个字符串指针,指向第一个参数,故可以连续为多个字符数组赋值。
1 2 3 4 5 6
| char str1[20] = "Hello, I'm C0dd1y"; char str2[20] = "C0dd1y";
strcpy(str2, strcpy(str1 + 11, "SugarMGP")); cout << str1 << endl; cout << str2 << endl;
|
另外,不能使用strcpy()
对没初始化的字符指针赋值。
1 2
| char* str; strcpy(str, "hello world");
|
程C老师给我们写的神金示例,好孩子千万不要这样写码哦:
1 2 3 4 5 6 7 8 9
| char s[81]="apple***\0s1234567"; char s2[81]="abc"; cout << s << endl; cout << s+9 << endl; strcpy(s,s2); cout << s << endl; cout << s+4 << endl; cout << s+9 << endl;
|
strcat() 函数
strcat()
函数用于将一个字符串追加到另一个字符串的末尾。
1 2
| char* strcat(char* s1, const char* s2);
|
strcat()
的返回值是一个字符串指针,同样指向第一个参数。
1 2 3 4
| char s1[12] = "hello"; char s2[6] = "world"; strcat(s1, strcat(s2, ", SugarMGP")); puts(s1);
|
同样也举个神金示例:
1 2 3 4 5 6 7 8 9 10 11 12
| char s[81]="apple***\0s1234567"; char s2[81]="abc"; cout << s << endl; cout << s+9 << endl;
strcat(s,s2); cout << s << endl; cout << s+12 << endl;
strcat(s+12,s2); cout << s << endl; cout << s+12 << endl;
|
strcmp() 函数
strcmp()
函数用于比较两个字符串的内容。
1 2
| int strcmp(const char* s1, const char* s2);
|
按照字典顺序,若s1
小于s2
,则返回负值;若s1
大于s2
,则返回正值;若s1
等于s2
,则返回零。
1 2 3 4 5
|
strcmp(s1, s2) strcmp(s1, s3)
|
strchr() 和 strstr()
strchr()
函数用于在字符串中查找某个字符,并返回该字符第一次出现的位置。
strstr()
函数用于在字符串中查找另一个字符串的第一次出现的位置。
1 2 3 4 5 6 7 8 9 10 11
| char str[] = "Hello, world!";
char* p = strchr(str, 'l'); if (p != NULL) { cout << p - str << endl; }
char* q = strstr(str, "world"); if (q != NULL) { cout << q - str << endl; }
|