STRSTR(3)
๐ STRSTR(3) โ the library-functions-manual girlies are gonna eat this up
okay so bestie. this is a page from the Library Functions Manual about strstr, strcasestr, and strnstr โ the "where is my substring" squad. they all live in the Standard C Library (libc, link with -lc). no cap, these are the finders of the string world ๐โจ
๐ NAME
strstr, strcasestr, strnstr โ locate a substring in a string
๐ LIBRARY
Standard C Library (libc, -lc)
โจ SYNOPSIS
like, here's the whole cast, okay ๐
#include <string.h>
char *
strstr(const char *big, const char *little);
char *
strcasestr(const char *big, const char *little);
char *
strnstr(const char *big, const char *little, size_t len);
#include <string.h>
#include <xlocale.h>
char *
strcasestr_l(const char *big, const char *little, locale_t loc);
๐ฆ DESCRIPTION
so the way this works:
strstr()finds the first time the null-terminated stringlittleshows up inside the null-terminated stringbig. main character search behavior, honestly.strcasestr()is literallystrstr()but it ignores the case of both strings. so "BAR" and "bar" are the same vibe to it ๐strcasestr_l()does the exact same thing asstrcasestr()but takes an explicit locale (locale_t loc) instead of just using whatever the current locale is. very "i brought my own settings" of it.strnstr()finds the first occurrence of the null-terminated stringlittlein the stringbig, but it searches not more thanlencharacters. any characters that show up after a'\0'character are not searched, periodt.
โ ๏ธ important vibe check: strnstr() is a FreeBSD specific API, so it should only be used when portability is not a concern. like, she's iconic but she does NOT travel well ๐ use her only when you don't need your code to run everywhere.
๐ซถ RETURN VALUES
okay so the returns, listen up:
- if
littleis an empty string โbigis returned - if
littleoccurs nowhere inbigโNULLis returned - otherwise โ you get a pointer to the first character of the first occurrence of
little
๐ธ EXAMPLES
first one sets the pointer ptr to the "Bar Baz" portion of largestring, slay:
const char *largestring = "Foo Bar Baz";
const char *smallstring = "Bar";
char *ptr;
ptr = strstr(largestring, smallstring);
and this next one sets the pointer ptr to NULL, because only the first 4 characters of largestring are searched. yeah. sad but correct ๐ญ
const char *largestring = "Foo Bar Baz";
const char *smallstring = "Bar";
char *ptr;
ptr = strnstr(largestring, smallstring, 4);
๐ SEE ALSO
go read these too, iykyk:
memchr(3), memmem(3), strchr(3), strcspn(3), strpbrk(3), strrchr(3), strsep(3), strspn(3), strtok(3), wcsstr(3)
โญ STANDARDS
strstr() conforms to ISO/IEC 9899:1990 (โISO C90โ). certified classic behavior ๐
๐ HISTORY
strnstr() was introduced by FreeBSD 4.5 and is non-standard. she's a specialty, not a standard-issue icon ๐
and that's the whole page, bestie ๐ง from the Debian build, dated October 11, 2001 โ old but still serving ๐
๐ฆ where this page comes from
- freebsd 15.1: base 15.1
- Copyright (c) 2001 Mike Barcroft <mike@FreeBSD.org>
- Copyright (c) 1990, 1991, 1993 The Regents of the University of California. All rights reserved
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms