STRSTR(3)

๐Ÿ“ฆ
version 15.1 ยท 2 versions
, in freebsd 15.1

๐Ÿ’… 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 string little shows up inside the null-terminated string big. main character search behavior, honestly.
  • strcasestr() is literally strstr() 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 as strcasestr() 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 string little in the string big, but it searches not more than len characters. 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 little is an empty string โ†’ big is returned
  • if little occurs nowhere in big โ†’ NULL is 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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms