strsep(3)
โจ strsep(3) โจ
okay bestie, we're in the Library Functions Manual today and the star of the show is strsep, the string separate queen ๐ she lives in the Standard C library (libc, -lc) and honestly? she's that girl. let's get into it ๐
๐ NAME ๐
strsep โ string separate, periodt.
๐ LIBRARY ๐
Standard C library (libc, -lc). yeah she's in libc, she's not some obscure little side quest ๐ซถ
๐ธ SYNOPSIS ๐ธ
#include <string.h>
char *strsep(char **restrict stringp, const char *restrict delim);
Feature Test Macro Requirements for glibc (go peep feature_test_macros(7) for the full tea โ):
strsep():- Since glibc 2.19:
_DEFAULT_SOURCE
- glibc 2.19 and earlier:
_BSD_SOURCE
- Since glibc 2.19:
๐ฆ DESCRIPTION ๐ฆ
okay so here's the vibe. if *stringp is NULL, strsep() just returns NULL and does literally nothing else. no drama, no side quests, she said "not today" ๐
otherwise? she goes hunting for the first token in the string *stringp that's delimited by one of the bytes in the string delim. and the way she does it is iconic: this token gets terminated by overwriting the delimiter with a null byte ('\0'), and then *stringp gets updated to point past the token. so yeah, she's out here editing in place, main character energy only โจ
and if no delimiter was found at all? no worries babes โ the token is just taken to be the entire string *stringp, and *stringp is made NULL. slay.
๐ RETURN VALUE ๐
strsep() returns a pointer to the token. which is, like, literally just the original value of *stringp. she gives you back what you gave her, but transformed ๐ฆ
โญ ATTRIBUTES โญ
for an explanation of the terms used in this section, go check attributes(7) bestie.
- Interface:
strsep()- Attribute: Thread safety
- Value:
MT-Safe
translation: she's MT-Safe, she can hang in a crowd, no cap ๐ซถ
๐ STANDARDS ๐
BSD.
๐ HISTORY ๐
4.4BSD.
and not me telling you that strsep() was introduced as a replacement for strtok(3) ๐ because strtok(3) literally cannot handle empty fields. that's the whole origin story. strtok said "no empty fields" and strsep said "bet" ๐
โ ๏ธ CAVEATS โ ๏ธ
be cautious when using this function, fr fr. if you do use it, note that: ๐
- the identity of the delimiting character is lost. like, gone. she overwrites it with a null byte, so that lil delimiter does not survive the encounter ๐ญ
๐ฅ EXAMPLES ๐ฅ
the program below is a port of the one found in strtok(3) โ except this one doesn't discard multiple delimiters or empty tokens. so you get to see every single one, including the empty little guys ๐
$ ./a.out 'a/bbb///cc;xxx:yyy:' ':;' '/'
1: a/bbb///cc
--> a
--> bbb
-->
-->
--> cc
2: xxx
--> xxx
3: yyy
--> yyy
4:
-->
๐ง Program source ๐ง
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main(int argc, char *argv[])
{
char *token, *subtoken;
if (argc != 4) {
fprintf(stderr, "Usage: %s string delim subdelim\n", argv[0]);
exit(EXIT_FAILURE);
}
for (unsigned int j = 1; (token = strsep(&argv[1], argv[2])); j++) {
printf("%u: %s\n", j, token);
while ((subtoken = strsep(&token, argv[3])))
printf("\t --> %s\n", subtoken);
}
exit(EXIT_SUCCESS);
}
๐ SEE ALSO ๐
and that's the whole page babes! this iconic lil number is from Linux man-pages 6.20, dated 2026-08-10, and yes she signs her name strsep(3) at the bottom like the queen she is ๐๐ go separate some strings, you got this ๐ซถโจ
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: Linux-man-pages-copyleft
- Copyright 1993, David Metcalfe <david@prism.demon.co.uk>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms