zoukankan      html  css  js  c++  java
  • C语言常用的字符串函数总结

    一、strlen

    1、原型:size_t strlen(char const* string);

    2、功能:返回字符串 string 的长度(不包含字符串终止符NUL)

    3、注意:size_t是一个无符号整数类型

    4、举例:

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 
     5 int main()
     6 {
     7     char* y = "abcdef";
     8     char* x = "abcd";
     9 
    10     if (strlen(x) >= strlen(y)) {
    11         printf("1: strlen(x) >= strlen(y)
    ");
    12     } else {
    13         printf("1: strlen(x) < strlen(y)
    ");
    14     }
    15 
    16     /* 由于strlen(x)返回的是一个size_t,所以strlen(x) - strlen(y) >= 0恒成立,
    17      * 导致出现错误的结论
    18      */
    19     if (strlen(x) - strlen(y) >= 0) { 
    20         printf("2: strlen(x) - strlen(y) >= 0
    ");
    21     } else {
    22         printf("2: strlen(x) - strlen(y) < 0
    ");
    23     }
    24     
    25     // 将size_t转换为int类型后,可以返回正确的值
    26     if ((int)(strlen(x)) - (int)(strlen(y)) >= 0) { 
    27         printf("3: (int)strlen(x) - strlen(y) >= 0
    ");
    28     } else {
    29         printf("3: (int)strlen(x) - strlen(y) < 0
    ");
    30     }
    31     
    32     return 0;
    33 }
    View Code

    运行结果:

    二、strcpy

    1、原型:char *strcpy(char *dst, char const *src);

    2、功能:将参数src字符串复制到dst参数中。如果参数src和dst在内存中出现重叠,其结果是未定义的。由于dst参数将进行修改,所以它必须是个字符数组或者是一个指向动态分配内存的数组的指针,不能使用字符串常量。返回参数dst的一份拷贝。

    3、注意:

    目标参数dst的以前内容将被覆盖并丢失。即使新的字符串比dst原先的内存更短,由于新字符串是以NUL字符结尾,所以老字符串最后剩余的几个字符也会被有效的删除。如果字符串比数组长,多余的字符仍被复制,它们将覆盖原先存储于数组后面的内存空间的值。所以必须保证目标字符数组的空间足以容纳需要复制的字符串。

    4、举例

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 
     5 int main()
     6 {
     7     char msg[] = "Original message";
     8     printf("before strcpy: msg:%s
    ", msg);
     9     strcpy(msg, "Different");
    10     printf("after strcpy: msg:%s
    ", msg);
    11     return 0;
    12 }
    View Code

    运行结果:

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 
     5 int main()
     6 {
     7     char msg[] = "Different";
     8     printf("before strcpy: msg:%s
    ", msg);
     9     strcpy(msg, "Original message");
    10     printf("after strcpy: msg:%s
    ",msg);
    11     return 0;
    12 }
    View Code

    运行结果:

     三、strncpy

    1、原型:char *strncpy(char *dst, char const *src, size_t len);

    2、功能:和strcpy一样,strncpy把源字符串的字符复制到目标数组。然而,它总是 正好向dst写入len个字符。如果strlen(src)的值小于len, dst数组就用额外的NUL字节填充到len长度。如果strlen(src)的值大于或者等于len,那么只有len个字符被复制到dst中。

    3、注意:strncpy调用的结果可能不是一个字符串,它的结果将不会以NUL字符结尾, 因此字符串必须以NUL字符结尾

    4、举例

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 
     5 #define STR_CPY_LEN 5
     6 int main()
     7 {
     8     char msg[] = "Original message";
     9     printf("before strncpy: msg:%s
    ", msg);
    10     strncpy(msg, "Different", STR_CPY_LEN);
    11     printf("after strncpy: msg:%s
    ",msg);
    12     return 0;
    13 }
    View Code

    运行结果:

    四、strcat

    1、原型:char *strcat(char *dst, char const *src);

    2、功能:将一个字符串添加(连接)到另一个字符串的后面。

    3、注意:src和dst的所指的内存区域不能重叠,如果发生重叠,其结果是未定义的。

    4、举例:

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 
     5 int main()
     6 {
     7     char msg[20] = "hello";
     8     printf("before strcat: msg:%s
    ", msg);
     9     strcat(msg, ", world.");
    10     printf("after strcat: msg:%s
    ", msg);
    11     return 0;
    12 }
    View Code

    运行结果:

    五、strncat

    1、原型:char *strncat(char *dst, char const *src, size_t len);

    2、功能:它从src最多复制 len个字符到dst中。但是, strncat总是在结果字符串后面添加一个NUL字符。

    3、注意:src和dst所指的内存区域不能重叠,并且dst必须有足够多的空间来容纳src的字符串。

    4、举例:

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 #define LEN 5
     5 int main()
     6 {
     7     char msg[20] = "hello";
     8     printf("before strcat: len: %d, msg:%s
    ", strlen(msg), msg);
     9     strncat(msg, ", world.", LEN);
    10     printf("after strcat: len: %d, msg:%s
    ", strlen(msg), msg);
    11     return 0;
    12 }
    View Code

    运行结果:

    六、strcmp

    1、原型:int strcmp(char const *s1, char const *s2);

    2、功能:比较两个字符串。如果s1小于s2,strcmp函数返回一个小于零的值。如果s1大于s2,函数返回一个大于零的值。如果两个字符串相等,函数就返回零。

    3、注意:由于strcmp并不修改它的任何一个参数,所以不存在溢出字符数组的危险。但是,和其他不受限制的字符串函数(strcpy, strcat)一样,strcmp函数的字符串参数也必须以一个NUL字符结尾。如果并非如此,strcmp就可能对参数后面的字符进行比较,这个比较结果将不会有什么意义。

    4、举例:

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 
     5 
     6 int main()
     7 {
     8     char *s1 = "hello";
     9     char *s2 = "hello, world";
    10     int ans = strcmp(s1, s2);
    11     if (ans > 0) {
    12         printf("s1 > s2
    ");
    13     } else if (ans < 0) {
    14         printf("s1 < s2
    ");
    15     } else {
    16         printf("s1 = s2
    ");
    17     }
    18     return 0;
    19 }
    View Code

    运行结果:

    七、strncmp

    1、原型:int strncmp(char const *s1, char const *s2, size_t len);

     2、功能:和strcmp一样,也用于比较两个字符串,但它最多比较 len 个字节。如果两个字符串在第 len 个字符之前存在不相等的字符,这个函数就像strcmp一样停止比较,返回结果。如果两个字符串的前len 个字符相等,函数就返回零。

    3、举例:

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 #define LEN 5
     5 
     6 int main()
     7 {
     8     char *s1 = "hello";
     9     char *s2 = "hello, world";
    10     int ans = strncmp(s1, s2, LEN);
    11     if (ans > 0) {
    12         printf("s1 > s2
    ");
    13     } else if (ans < 0) {
    14         printf("s1 < s2
    ");
    15     } else {
    16         printf("s1 = s2
    ");
    17     }
    18     return 0;
    19 }
    View Code

    返回结果:

    八、strchr、strrchr

    1、原型:char *strchr(char const *str, int ch);

          char *strrchr(char const *str, int ch);

    2、功能:在一个字符串中查找一个特定字符。

    3、注意:第2个参数是一个整型值。但是,它包含了一个字符值。strchr在字符串str中查找字符ch第一次出现的位置,找到后函数返回一个指向该位置的指针。如果该字符并不存在于str中,函数就返回一个NULL指针。strrchr的功能和strchr基本一致,只是它所返回的是一个指向字符串中该字符最后一次出现的位置(最右边那个)。

    4、举例:

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 int main()
     5 {
     6     char string[20] = "Hello there, honey.";
     7     char *ans;
     8     char *ans1;
     9     ans = strchr(string, 'h');
    10     printf("ans:|%s|
    ", ans);
    11     ans1 = strrchr(string, 'h');
    12     printf("ans:|%s|
    
    ", ans1);
    13 
    14     ans = strchr(string, 'a');
    15     printf("ans:|%s|
    ", ans);
    16     ans1 = strrchr(string, 'a');
    17     printf("ans:|%s|
    ", ans1);
    18     return 0;
    19 }
    View Code

    运行结果:

    九、strpbrk

    1、原型:char *strpbrk(char const *str, char const *group);

    2、功能:这个函数返回一个指向str中第1个匹配group中任何一个字符的字符位置。如果未找到匹配,函数返回一个NULL指针。

    3、举例:

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 int main()
     5 {
     6     char string[20] = "Hello there, honey.";
     7     char *ans;
     8     ans = strpbrk(string, "aeiou");
     9     printf("ans:|%s|
    ", ans);
    10     return 0;
    11 }
    View Code

    运行结果:

     十、strstr

    1、原型:char *strstr(char *s1, char *s2);

    2、功能:这个函数在s1中查找整个s2第1次出现的起始位置,并返回一个指向该位置的指针。如果s2并没有完整地出现在s1的任何地方,函数将返回一个NULL指针。如果第2个参数是一个空字符串,函数就返回s1。

    3、举例:

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 int main()
     5 {
     6     char s1[20] = "Hello there, honey.";
     7     char s2[] = "honey";
     8     char *s3 = "";
     9     char *ans;
    10     ans = strstr(s1, s2);
    11     printf("ans:|%s|
    ", ans);
    12 
    13     ans = strstr(s1, s3);
    14     printf("ans:|%s|
    ", ans);
    15     return 0;
    16 }
    View Code

    运行代码:

    标准库中并不存在strrstr或者strrpbrk函数。下面是其中的一个实现:

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 char *My_strrstr(char const *s1, char const *s2)
     5 {
     6     char *last = NULL;
     7     char *current = NULL;
     8 
     9     /* 只在第2个字符串不为空时才进行查找,如果s2为空,返回NULL */
    10     if (*s2 != '') {
    11         /* 查找s2在s1中第1次出现的位置 */
    12         current = strstr(s1, s2);
    13 
    14         /* 我们每次找到字符串时,让指针指向它的起始位置,然后查找该字符串下一个匹配位置 */
    15         while (current != NULL) {
    16             last = current;
    17             current = strstr(last + 1, s2);
    18         }
    19     }
    20     return last;
    21 }
    22 
    23 char *My_strrpbrk(char const *s1, char const *group)
    24 {
    25     char *last = NULL;
    26     char *current = NULL;
    27 
    28     /* 只在第2个字符串不为空时才进行查找,如果s2为空,返回NULL */
    29     if (*group != '') {
    30         /* 查找s2在s1中第1次出现的位置 */
    31         current = strpbrk(s1, group);
    32 
    33         /* 我们每次找到字符串时,让指针指向它的起始位置,然后查找该字符串下一个匹配位置 */
    34         while (current != NULL) {
    35             last = current;
    36             current = strpbrk(last + 1, group);
    37         }
    38     }
    39     return last;
    40 }
    41 
    42 int main()
    43 {
    44     char s1[30] = "Hello there, honey.honey";
    45     char s2[] = "honey";
    46     char *s3 = "";
    47     char *ans;
    48     ans = strstr(s1, s2);
    49     printf("ans:|%s|
    ", ans);
    50 
    51     ans = strstr(s1, s3);
    52     printf("ans:|%s|
    ", ans);
    53 
    54     ans = My_strrstr(s1, s2);
    55     printf("ans:|%s|
    ", ans);
    56 
    57     ans = strpbrk(s1, "e");
    58     printf("ans:|%s|
    ", ans);
    59 
    60     ans = My_strrpbrk(s1, "e");
    61     printf("ans:|%s|
    ", ans);
    62 
    63     return 0;
    64 }
    View Code

    运行结果:

    十一、strtok

    1、原型:char *strtok(char *str, char const *sep);

    2、功能:分解字符串str为一组字符串,分隔符为sep。

    3、注意:如果strtok函数的第1个参数不是NULL,函数将找到字符串的第1个标记。strtok同时将保存它在字符串中的位置。如果strtok函数的第1个参数是NULL,函数就在同一个字符串中从这个被保存的位置开始像前面一样查找下一个标记。如果字符串内不存在更多的标记,strtok函数就返回一个NULL指针。在典型情况下,在第1次调用strtok时,向它传递一个指向字符串的指针。然后,这个函数被重复调用(第1个参数为NULL),直到它返回NULL为止。

    4、举例:

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 int main()
     5 {
     6     char whitespace[] = " ";
     7     char *token;
     8     char line[] = "I love you";
     9     for (token = strtok(line, whitespace); token !=NULL;
    10          token = strtok(NULL, whitespace)) {
    11              printf("Next token is |%s|
    ", token);
    12         }
    13     return 0;
    14 }
    View Code

    运行结果:

  • 相关阅读:
    logstash收集nginx日志
    logstash收集java日志,多行合并成一行
    一个配置文件收集多个日志-if根据type类型判断
    CentOS 7 kibana安装配置
    CentOS7 logstash配置部署
    Centos7 Elasticsearch部署
    awk命令
    top命令
    java中的getClass()函数
    java容器
  • 原文地址:https://www.cnblogs.com/LydiammZuo/p/11747771.html
Copyright © 2011-2022 走看看