1strsep(3) Library Functions Manual strsep(3)
2
3
4
6 strsep - extract token from string
7
9 Standard C library (libc, -lc)
10
12 #include <string.h>
13
14 char *strsep(char **restrict stringp, const char *restrict delim);
15
16 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
17
18 strsep():
19 Since glibc 2.19:
20 _DEFAULT_SOURCE
21 glibc 2.19 and earlier:
22 _BSD_SOURCE
23
25 If *stringp is NULL, the strsep() function returns NULL and does noth‐
26 ing else. Otherwise, this function finds the first token in the string
27 *stringp that is delimited by one of the bytes in the string delim.
28 This token is terminated by overwriting the delimiter with a null byte
29 ('\0'), and *stringp is updated to point past the token. In case no
30 delimiter was found, the token is taken to be the entire string
31 *stringp, and *stringp is made NULL.
32
34 The strsep() function returns a pointer to the token, that is, it re‐
35 turns the original value of *stringp.
36
38 For an explanation of the terms used in this section, see at‐
39 tributes(7).
40
41 ┌────────────────────────────────────────────┬───────────────┬─────────┐
42 │Interface │ Attribute │ Value │
43 ├────────────────────────────────────────────┼───────────────┼─────────┤
44 │strsep() │ Thread safety │ MT-Safe │
45 └────────────────────────────────────────────┴───────────────┴─────────┘
46
48 None.
49
51 4.4BSD.
52
53 The strsep() function was introduced as a replacement for strtok(3),
54 since the latter cannot handle empty fields. However, strtok(3) con‐
55 forms to C89/C99 and hence is more portable.
56
58 Be cautious when using this function. If you do use it, note that:
59
60 • This function modifies its first argument.
61
62 • This function cannot be used on constant strings.
63
64 • The identity of the delimiting character is lost.
65
67 The program below is a port of the one found in strtok(3), which, how‐
68 ever, doesn't discard multiple delimiters or empty tokens:
69
70 $ ./a.out 'a/bbb///cc;xxx:yyy:' ':;' '/'
71 1: a/bbb///cc
72 --> a
73 --> bbb
74 -->
75 -->
76 --> cc
77 2: xxx
78 --> xxx
79 3: yyy
80 --> yyy
81 4:
82 -->
83
84 Program source
85
86 #include <stdio.h>
87 #include <stdlib.h>
88 #include <string.h>
89
90 int
91 main(int argc, char *argv[])
92 {
93 char *token, *subtoken;
94
95 if (argc != 4) {
96 fprintf(stderr, "Usage: %s string delim subdelim\n", argv[0]);
97 exit(EXIT_FAILURE);
98 }
99
100 for (unsigned int j = 1; (token = strsep(&argv[1], argv[2])); j++) {
101 printf("%u: %s\n", j, token);
102
103 while ((subtoken = strsep(&token, argv[3])))
104 printf("\t --> %s\n", subtoken);
105 }
106
107 exit(EXIT_SUCCESS);
108 }
109
111 memchr(3), strchr(3), string(3), strpbrk(3), strspn(3), strstr(3), str‐
112 tok(3)
113
114
115
116Linux man-pages 6.04 2023-03-30 strsep(3)