字符串处理相关函数
sizeof
和strlen
的区别:
sizeof
是 C 语言内置的操作符关键字,而strlen
是 C 语言库函数;sizeof
仅用于计算数据类型的大小或者变量的大小,而strlen
只能以结尾为’ \0 '的字符串作为参数;- 编译器在编译时就计算出了
sizeof
的结果,而strlen
必须在运行时才能计算出来; sizeof
计算数据类型或变量会占用内存的大小,strlen
计算字符串实际长度。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char str[50] = "Linux app strlen test!";
char *ptr = str;
printf("sizeof: %ld\n", sizeof(str));
printf("strlen: %ld\n", strlen(str));
puts("~~~~~~~~~~");
printf("sizeof: %ld\n", sizeof(ptr));
printf("strlen: %ld\n", strlen(ptr));
exit(0);
}
-
strcat()
函数和strncat()
函数
strcat()
函数会把src
所指向的字符串追加到dest
所指向的字符串末尾,所以必须要保证dest
有足够的存储空间来容纳两个字符串,否则会导致溢出错误;dest
末尾的’ \0 ‘结束字符会被覆盖,src
末尾的结束字符’ \0 ‘会一起被复制过去,最终的字符串只有一个’ \0 '。#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char str1[100] = "Linux app strcat test, "; char str2[] = "Hello World!"; strcat(str1, str2); puts(str1); exit(0); }
strncat()
与strcat()
的区别在于,strncat
可以指定源字符串追加到目标字符串的字符数量。如果源字符串src
包含 n 个或更多个字符,则strncat()
将 n+1 个字节追加到dest
目标字符串(src
中的 n个字符加上结束字符’ \0 ')。#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char str1[100] = "Linux app strcat test, "; char str2[] = "Hello World!"; strncat(str1, str2, 5); puts(str1); exit(0); }
-
strcpy()
函数和strncpy()
函数
strcpy()
会把src
(必须包含结束字符’ \0 ‘)指向的字符串复制(包括字符串结束字符’ \0 ')到dest
,所以必须保证dest
指向的内存空间足够大,能够容纳下src
字符串,否则会导致溢出错误。#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char str1[100] = {0}; char str2[] = "Hello World!"; strcpy(str1, str2); puts(str1); exit(0); }
strncpy()
与strcpy()
的区别在于,strncpy()
可以指定从源字符串src
复制到目标字符串dest
的字符数量。把src
所指向的字符串复制到dest
,最多复制 n 个字符。当 n 小于或等于src
字符串长度(不包括结束字符的长度)时,则复制过去的字符串中没有包含结束字符’ \0 ‘;当 n 大于src
字符串长度时,则会将src
字符串的结束字符’ \0 '也一并拷贝过去,必须保证dest
指向的内存空间足够大,能够容纳下拷贝过来的字符串,否则会导致溢出错误。#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char str1[100] = "AAAAAAAAAAAAAAAAAAAAAAAA"; char str2[] = "Hello World!"; strncpy(str1, str2, 5); puts(str1); puts("~~~~~~~~~~~~~~~"); strncpy(str1, str2, 20); puts(str1); exit(0); }
-
memset
函数memset()
函数用于将某一块内存的数据全部设置为指定的值。参数 c 虽然是以int
类型传递,但memset()
函数在填充内存块时是使用该值的无符号字符形式,也就是函数内部会将该值转换为unsigned char
类型的数据,以字节为单位进行数据填充。#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char str[100]; memset(str, 0x0, sizeof(str)); exit(0); }
-
strcmp()
和strncmp()
函数
int strcmp(const char *s1, const char *s2);
- 如果返回值小于 0,则表示
str1
小于str2
- 如果返回值大于 0,则表示
str1
大于str2
- 如果返回值等于 0,则表示字符串
str1
等于字符串str2
strcmp
进行字符串比较,主要是通过比较字符串中的字符对应的 ASCII 码值,strcmp
会根据 ASCII 编码依次比较str1
和str2
的每一个字符,直到出现了不同的字符,或者某一字符串已经到达末尾(遇见了字符串结束字符’ \0 ')。#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { printf("%d\n", strcmp("ABC", "ABC")); printf("%d\n", strcmp("ABC", "a")); printf("%d\n", strcmp("a", "ABC")); exit(0); }
strncmp()
与strcmp()
函数一样,也用于对字符串进行比较操作,但最多比较前 n 个字符。#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { printf("%d\n", strncmp("ABC", "ABC", 3)); printf("%d\n", strncmp("ABC", "ABCD", 3)); printf("%d\n", strncmp("ABC", "ABCD", 4)); exit(0); }
- 如果返回值小于 0,则表示
-
strchr
函数使用
strchr()
函数可以查找到给定字符串当中的某一个字符。字符串结束字符’ \0 ‘也将作为字符串的一部分,因此,如果将参数 c 指定为’ \0 ',则函数将返回指向结束字符的指针。
strchr
函数在字符串 s 中从前到后(或者称为从左到右)查找字符 c,找到字符 c 第一次出现的位置就返回,返回值指向这个位置,如果找不到字符 c 就返回 NULL。#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char *ptr = NULL; char str[] = "Hello World!"; ptr = strchr(str, 'W'); if (NULL != ptr) { printf("Character: %c\n", *ptr); printf("Offset: %ld\n", ptr - str); } exit(0); }
-
strrchr
函数strrchr()
与strchr()
函数一样,它同样表示在字符串中查找某一个字符,返回字符第一次在字符串中出现的位置,如果没找到该字符,则返回值 NULL,但两者唯一不同的是,strrchr()
函数在字符串中是从后到前(或者称为从右向左)查找字符,找到字符第一次出现的位置就返回,返回值指向这个位置。#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char *ptr = NULL; char str[] = "I love my home"; ptr = strchr(str, 'o'); if (NULL != ptr) printf("strchr: %ld\n", ptr - str); ptr = strrchr(str, 'o'); if (NULL != ptr) printf("strrchr: %ld\n", ptr - str); exit(0); }
-
strstr
函数char *strstr(const char *haystack, const char *needle);
与
strchr()
函数不同的是,strstr()
可在给定的字符串 haystack 中查找第一次出现子字符串 needle 的位置,不包含结束字符’ \0 '。#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char *ptr = NULL; char str[] = "I love my home"; ptr = strstr(str, "home"); if (NULL != ptr) { printf("String: %s\n", ptr); printf("Offset: %ld\n", ptr - str); } exit(0); }
-
atoi
、atol
、atoll
函数atoi()
、atol()
、atoll()
三个函数可用于将字符串分别转换为int
、long int
以及long long
类型的数据。使用这些函数需要包含头文件<stdlib.h>
。使用
atoi()
、atol()
、atoll()
函数只能转换十进制表示的数字字符串,即 0~9。#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { printf("atoi: %d\n", atoi("500")); printf("atol: %ld\n", atol("500")); printf("atoll: %lld\n", atoll("500")); exit(0); }
-
strtol
、strtoll
函数strtol()
、strtoll()
两个函数可分别将字符串转为long int
类型数据和 long long ing 类型数据,与atol()
、atoll()之间的区别在于,strtol()
、strtoll()
可以实现将多种不同进制数(譬如二进制表示的数字字符串、八进制表示的数字字符串、十六进制表示的数数字符串)表示的字符串转换为整形数据。需要进行转换的目标字符串可以以任意数量的空格或者 0 开头,转换时跳过前面的空格字符,直到遇上数字字符或正负符号(’ + ‘或’ - ‘)才开始做转换,而再遇到非数字或字符串结束时(’ /0 ')才结束转换,并将结果返回。
在 base=0 的情况下,如果字符串包含一个了“0x”前缀,表示该数字将以 16 为基数;如果包含的是“0”前缀,表示该数字将以 8 为基数。
当 base=16 时,字符串可以使用“0x”前缀。
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { printf("strtol: %ld\n", strtol("0x500", NULL, 16)); printf("strtol: %ld\n", strtol("0x500", NULL, 0)); printf("strtol: %ld\n", strtol("500", NULL, 16)); printf("strtol: %ld\n", strtol("0777", NULL, 8)); printf("strtol: %ld\n", strtol("0777", NULL, 0)); printf("strtol: %ld\n", strtol("1111", NULL, 2)); printf("strtol: %ld\n", strtol("-1111", NULL, 2)); exit(0); }
-
strtoul
、strtoull
函数这两个函数使用方法与 strtol()、strtoll()一样,就是返回值不同。
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { printf("strtoul: %lu\n", strtoul("0x500", NULL, 16)); printf("strtoul: %lu\n", strtoul("0x500", NULL, 0)); printf("strtoul: %lu\n", strtoul("500", NULL, 16)); printf("strtoul: %lu\n", strtoul("0777", NULL, 8)); printf("strtoul: %lu\n", strtoul("0777", NULL, 0)); printf("strtoul: %lu\n", strtoul("1111", NULL, 2)); exit(0); }
-
atof
函数atof()
用于将字符串转换为一个 double 类型的浮点数据。#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { printf("atof: %lf\n", atof("0.123")); printf("atof: %lf\n", atof("-1.1185")); printf("atof: %lf\n", atof("100.0123")); exit(0); }
-
strtod
、strtof
、strtold
函数、strtof()
、strtod()
以及strtold()
三个库函数可分别将字符串转换为float
类型数据、double
类型数据、longdouble
类型数据。#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { printf("strtof: %f\n", strtof("0.123", NULL)); printf("strtod: %lf\n", strtod("-1.1185", NULL)); printf("strtold: %Lf\n", strtold("100.0123", NULL)); exit(0); }
-
sprintf()
或snprintf()
函数数字转换为字符串。
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char str[20] = {0}; sprintf(str, "%d", 500); puts(str); memset(str, 0x0, sizeof(str)); sprintf(str, "%f", 500.111); puts(str); memset(str, 0x0, sizeof(str)); sprintf(str, "%u", 500); puts(str); exit(0); }