迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > C语言 >

使用 C 语言中的 strsep 函数

作者:迹忆客 最近更新:2023/03/28 浏览次数:

本文将演示如何在 C 语言中使用 strsep 函数的多种方法。


使用 strsep 函数在字符串中查找给定标记

strsep 是 C 标准库字符串实用程序的一部分,定义在 <string.h> 头文件中。它可以用来从字符串对象中提取被给定定界符包围的标记。

strsep 需要两个参数-指向 char*的指针和指向 char 的指针。第一个参数用于传递需要搜索的字符字符串的地址。第二个参数指定了一组定界符,用来标记提取的标记的开始和结束。请注意,在提取的标记字符串中,定界符会被丢弃。当找到第一个标记时,第一个参数被修改为存储指向下一个定界符的指针。

在下面的例子中,我们演示了如何使用对 strsep 的单次调用,提取两个以给定字符定界的标记。请注意,程序从 2 个命令行参数中获取字符串和定界符集,如果没有提供所需的参数,则以失败退出。接下来,我们用 strdupa 函数调用复制字符串,因为 strsep 修改了传递的指针,我们不想丢失原始值。strdupa 在栈上分配动态内存,调用者不应该释放从它返回的指针。

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
    char *str1, *token;

    if (argc != 3) {
        fprintf(stderr, "Usage: %s string delim\n",
                argv[0]);
        exit(EXIT_FAILURE);
    }

    str1 = strdupa(argv[1]);
    if (!str1)
        exit(EXIT_FAILURE);

    token = strsep(&str1, argv[2]);
    if (token == NULL)
        exit(EXIT_FAILURE);

    printf("extracted: '%s'\n", token);
    printf("left: '%s'\n", str1);

    exit(EXIT_SUCCESS);
}

示例命令:

./program "hello there" t

输出:

extracted: 'hello '
left: 'here'

或者,我们可以实现 for 循环,连续调用 strsep 函数,并提取每一个带有给定定界符的标记,而不是只提取最先遇到的标记-如前面的示例代码所示。但请注意,当一行中有两个定界符时,strsep 会返回一个空字符串;因此,调用者有责任在处理结果标记之前检查这一点。类似的功能也可以用 strtokstrtok_r 库函数来提供,但略有不同,在这个页面上有详细描述。

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
    char *str1, *token;

    if (argc != 3) {
        fprintf(stderr, "Usage: %s string delim\n",
                argv[0]);
        exit(EXIT_FAILURE);
    }

    str1 = strdupa(argv[1]);
    if (!str1)
        exit(EXIT_FAILURE);

    for (int j = 1; ; j++) {
        token = strsep(&str1, argv[2]);
        if (token == NULL)
            break;
        printf("%d: '%s'\n", j, token);
    }

    exit(EXIT_SUCCESS);
}

示例命令:

./program "hello there" tl

输出:

1: 'he'
2: ''
3: 'o '
4: 'here'

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

Vue - An In-Depth Guide to Lifecycle Hooks

发布时间:2025/02/21 浏览次数:117 分类:Vue

Vue has many lifecycle hooks, and it can be confusing to understand the meaning or purpose of each one. In this article, we will explain the function of each lifecycle hook and how to use them.

Solution for Flickering During Vue Template Parsing

发布时间:2025/02/21 浏览次数:103 分类:Vue

Solution for Flickering During Vue Template Parsing, Recently, while working on a project, I noticed that when the internet speed is slow, the screen flickers and the expression message appears. This happens because when the internet speed i

Pandas read_csv()函数

发布时间:2024/04/24 浏览次数:254 分类:Python

Pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 DataFrame 中。

Pandas 追加数据到 CSV 中

发布时间:2024/04/24 浏览次数:352 分类:Python

本教程演示了如何在追加模式下使用 to_csv()向现有的 CSV 文件添加数据。

Pandas loc vs iloc

发布时间:2024/04/24 浏览次数:837 分类:Python

本教程介绍了如何使用 Python 中的 loc 和 iloc 从 Pandas DataFrame 中过滤数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便