String Manipulation

来源:互联网 发布:mac qingli ds store 编辑:程序博客网 时间:2024/06/05 14:34

String Manipulation

目录

    • String Manipulation
    • 目录
    • Overview
    • strlen
      • Prototype
      • Description
      • Return Value
      • Example
    • strcmp strncmp
      • Prototype
      • Description
      • Return Value
      • Example
    • strcat strncat
      • Prototype
      • Description
      • Return Value
      • Example
    • strchr strrchr
      • Prototype
      • Description
      • Return Value
      • Example
    • strcpy strncpy
      • Prototype
      • Descpription
      • Return Value
      • Example
    • strspn strcspn
      • Prototype
      • Description
      • Return Value
      • Example
    • strstr
      • Prototype
      • Descprition
      • Return Value
      • Example
    • strtok
      • Prototype
      • Descprition
      • Return Value
      • Example


Overview

之前的文章 Strings 提到过,C 语言中的字符串就是内存中的字节序列,以 NULL (‘\0’) 结尾。这个 NULL 字符至关重要,因为它使得所有的这些字符串操作函数(比如 printf(), puts(), 以及其他所有的操作字符串的函数)知道字符串在何处结尾。

下面,我们就来讨论下这些字符串操作函数:截出子字符串、把若干字符串连接起来、获取字符串长度等等。


strlen()

返回字符串长度

Prototype

#include <string.h>size_t strlen(const char*s);

Description

这个函数返回一个字符串的长度(不包含 NULL 结尾符)。它是遍历整个字符串,知道碰到 NULL 字符,所以有点耗时间。如果你需要重复获得相同字符串的长度,最好使用 strlen() 之后,把长度值保存在一个变量中。

Return Value

返回字符串中的字符个数

Example

char *s = "Hello, world!"; // 13 characters// prints "The string is 13 characters long.":printf("The string is %d characters long.\n", strlen(s));

strcmp(), strncmp()

比较两个字符串并且返回一个差值。

Prototype

#include <string.h>int strcmp(const char *s1, const char *s2);int strncmp(const char *s1, const char *s2, size_t n);

Description

两个函数均是对两个字符串进行比较。strcmp() 比较整个字符串,而 strncmp() 仅仅比较字符串的前 n 个字符。

返回的差值要说明一下:
- 如果两个字符串相同,返回 0,否则
- 如果 s1 小于 s2,返回的是 负数,否则
- 返回的是正数

通常情况下,只要检查返回值是不是 0 就行了,因为人们一般只关心两个字符串是不是相同。

Return Value

如果两个字符串相等,返回0;如果 s1 小于 s2,返回 负数;如果 s1 大于 s2 返回正数。

Example

char *s1 = "Muffin";char *s2 = "Muffin Sandwich";char *s3 = "Muffin";strcmp("Biscuits", "Kittens"); // returns < 0 因为 'B' < 'K'strcmp("Kittens", "Biscuits"); // returns > 0 因为 'K' > 'B'if (strcmp(s1, s2) == 0)    printf("This won't get printed because the strings differ");if (strcmp(s1, s3) == 0)    printf("This will print because s1 and s3 are the same");if (!strcmp(s1, s3))    printf("The strings are the same!")if (!strncmp(s1, s2, 6))    printf("The first 6 characters of s1 and s2 are the same");

strcat(), strncat()

把两个字符串连成一个

Prototype

#include <string.h>char* strcat(const char *dest, const char *src);char* strncat(const char *dest, const char *src, size_t n);

Description

“Concatenate”,就是连接的意思。这俩函数把持有两个字符串,然后把它们连接起来,存储在第一个字符串中。

值得注意的是,这俩函数都没有考虑第一个字符串的长度问题。把一个 2M 的字符串存储在一个 10B 的空间里,结果一定会非常的酸爽。

一定要检查字符串的长度问题。不然会出现意想不到的结果。

strncat() 把 src 的前 n 个字符添加到 dest 的结尾处(覆盖 dest 结尾处的 ‘\0’) 并添加 ‘\0’。

Return Value

两个函数均返回指向 destination 字符串的指针

Example

char dest[20] = "Hello";char *src = ", World!";char numbers[] = "12345678";printf("dest before strcat: \"%s\"\n", dest); // "Hello"strcat(dest, src);printf("dest after strcat:  \"%s\"\n", dest); // "Hello, world!"strncat(dest, numbers, 3); // strcat first 3 chars of numbersprintf("dest after strncat: \"%s\"\n", dest); // "Hello, world!123"

strchr(), strrchr()

在字符串中查询字符

Prototype

#include <string.h>char *strchr(char *str, int c);char *strrchr(char *str, int c);

Description

strchr() 和 strrchr() 在字符串中查找首次或者最后出现的一个字符( “r” 是 “reverse” 的意思,从后往前找)。每个函数都会返回指向找到的字符的指针,如果没找到,返回 NULL。

如果想找下一次出现的位置,那么可以再次调用函数并且参数设置成上一次的返回值加 1(正向找)。或者减 1 (反向找的话)。如果正好到了字符串的末尾了,那要小心了,不然可能会溢出。

Return Value

返回指向找到的字符的指针,如果没找到,返回 NULL。

Example

// "Hello, world!"//       ^  ^   //       A  Bchar *str = "Hello, world!";char *p;p = strchr(str, ','); // p now points at position Ap = strrchr(str, 'o'); // p now points at position B// repeatedly find all occurances of the letter 'B'char *str = "A BIG BROWN BAT BIT BEEJ";char *p;for(p = strchr(str, 'B'); p != NULL; p = strchr(p + 1, 'B')) {    printf("Found a 'B' here: %s\n", p);}// output is://// Found a 'B' here: BIG BROWN BAT BIT BEEJ// Found a 'B' here: BROWN BAT BIT BEEJ// Found a 'B' here: BAT BIT BEEJ// Found a 'B' here: BIT BEEJ// Found a 'B' here: BEEJ

strcpy(), strncpy()

拷贝一个字符串

Prototype

#include <string.h>char *strcpy(char *dest, char *src);char *strncpy(char *dest, char *src, size_t n);

Descpription

这俩函数从一个地址处拷贝一个字符串到另一个地址,在 src 字符串的 NULL 处停止拷贝。

strncpy() 与 strcpy() 很像,唯一的区别就是只拷贝 src 的前 n 个字符。

请注意

如果 src 没有 n 个字符,那么 dest 字符串将不会以 NULL 结尾。一定要小心。这个时候 strncpy() 的表现与 strcpy() 是一样的。

可以手动给字符串添加结束字符 ‘\0’:

char s[10];char foo = "My hovercraft is full of eels."; // more than 10 charsstrncpy(s, foo, 9); // only copy 9 chars into positions 0-8s[9] = '\0';        // position 9 gets the terminator

Return Value

两个函数均返回指向目标字符串 dest 的指针

Example

char *src = "hockey hockey hockey hockey hockey hockey hockey hockey";char dest[20];int len;strcpy(dest, "I like "); // dest is now "I like "len = strlen(dest); // Be aware the differences between strlen() and sizeof()strncpy(dest+len, src, sizeof(dest)-len-1);// remember that sizeof() returns the size of the array in bytes// and a char is a byte:dest[sizeof(dest)-1] = '\0'; // terminate// dest is now:       v null terminator// I like hockey hocke 

strspn(), strcspn()

返回字符串中第一个不在 (strspn()) 或者 在 (strcspn()) 指定字符串中出现的字符下标

Prototype

#include <string.h>size_t strspn(char *str, const char *accept);size_t strcspn(char *str, const char *reject);

Description

strspn() 函数告诉你 src 字符串中第一个不在 accept 字符串中出现的字符的下标。
strcspn() 函数告诉你 src 字符串中第一个在 reject 字符串中出现的字符的下标

Return Value

返回字符串中第一个不在 (strspn()) 或者 在 (strcspn()) 指定字符串中出现的字符下标

Example

char str1[] = "a banana";char str2[] = "the bolivian navy on manuvers in the south pacific";// str1 中第一个不在 "aeiou" 中的字符下标?n = strspn(str1, "aeiou");  // n == 1,"e"// str1 中第一个不在 "ab " 中的字符下标?n = strspn(str1, "ab "); // n == 4,"n"// str2 中第一个在 "y" 中的字符下标?n = strcspn(str2, "y"); // n = 16, "y"

strstr()

在一个字符串中查找另一个字符串

Prototype

#include <string.h>char *strstr(const char *str, const char *substr);

Descprition

在 str 字符串中查找 substr,返回指向 substr 的指针。

Return Value

返回指向 substr 的指针,或者是 NULL(如果没找到)。

Example

char *str = "The quick brown fox jumped over the lazy dogs.";char *p;p = strstr(str, "lazy");printf("%s\n", p); // "lazy dogs."// p is NULL after this, since the string "wombat" isn't in str:p = strstr(str, "wombat");

strtok()

分割字符串

Prototype

#include <string.h>char *strtok(char *str, const char *delim);

Descprition

如果有一个字符串里面有很多分隔符,想要把这个字符串分割成一小段一小段,那么这个函数就派上用场了。

strtok() 的使用方式有点特别,其实是有点怪异。

str 是将要被分割的字符串,第一次调用 strtok() 时,str 已经被分割成一个一个的小片段了。为了获得更多的分割的片段,需要多次调用 strtok(),但是需要传入 NULL。这是它诡异的地方,但是 strtok() 记住了初始传入的 str 字符串,然后不断的分割它。

需要注意的是,str 字符串在调用过程中会被破坏掉,所以如果想不被修改,需要传入 str 的副本,这样 strtok() 就不会破坏原始字符串了。

Return Value

一个指向下一个 分隔片段 的指针,如果没了,就返回 NULL。

Example

char str[] = "Where is my bacon, dude?";char *token;// 注意,在使用 strtok() 时,下面的 if-do-while 结构// 会经常经常经常见到!!!// 获取第一个token (making sure there is a first token!)if ((token = strtok(str, ".,?! ")) != NULL) {    do {        printf("Word: \"%s\"\n", token);        // while循环继续获取下一个 token        // (传入 NULL 作为第一个参数)    } while ((token = strtok(NULL, ".,?! ")) != NULL);}// output:Word: "Where"Word: "is"Word: "my"Word: "bacon"Word: "dude"
0 0
原创粉丝点击