String Manipulation

String Manipulation

目录


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 numbers
printf("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  B

char *str = "Hello, world!";
char *p;

p = strchr(str, ','); // p now points at position A
p = 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 chars

strncpy(s, foo, 9); // only copy 9 chars into positions 0-8
s[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"

转载于:https://www.cnblogs.com/1202zhyl/p/5726839.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值