strstr(3bsd)

πŸ“¦
version 0.12.2 Β· 3 versions
, in alpine 3.22, alpine 3.23, alpine 3.24, arch rolling, debian trixie, epel 10, epel 9, fedora 43, fedora 44, nixos 26.05, ubuntu noble, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸ’– strstr(3bsd) β€” the LOCAL girlies' guide πŸ’–

okay so bestie, welcome to strstr(3bsd), from the <cyan>LOCAL</cyan> section β€” yes she's local, she's niche, she's that girl πŸŽ€


✨ NAME ✨

strnstr β€” locate a substring in a string πŸ”

πŸ“š LIBRARY πŸ“š

Utility functions from BSD systems, aka libbsd, and you link her with -lbsd πŸ’…

🧚 SYNOPSIS 🧚

#include <string.h>
(See libbsd(7) for include usage.)

char *
strnstr(const char *big, const char *little, size_t len);

yes you literally need to peep libbsd(7) for the include usage, i don't make the rules, i just report them 🫢

🌸 DESCRIPTION 🌸

so the way strnstr() works is: she goes looking for the first occurrence of the null-terminated string little inside the string big, and she only searches not more than len characters. that's the whole vibe check ✨

anything that shows up after a '\0' character? not searched. periodt. she stops caring right there πŸ’€

and listen β€” strnstr() is a FreeBSD specific API, so it should only be used when portability is not a concern. if you're trying to be portable bestie, this is not the function for you 😭

πŸŽ€ RETURN VALUES πŸŽ€

here's the tea on what she gives you back:

  • if little is an empty string, you get big back. iconic behavior honestly πŸ’–
  • if little occurs nowhere in big, you get NULL. sad. tragic. we move on πŸ™ˆ
  • otherwise? you get a pointer to the first character of the first occurrence of little. slay βœ¨πŸ‘‘

πŸ¦‹ EXAMPLES πŸ¦‹

okay so this next one sets the pointer ptr to NULL, because only the first 4 characters of largestring are searched. watch πŸ‘€

const char *largestring = "Foo Bar Baz";
const char *smallstring = "Bar";
char *ptr;

ptr = strnstr(largestring, smallstring, 4);

she searched four characters. "Bar" was not in those four characters. therefore: NULL. no cap. πŸ’€

πŸ‘‘ SEE ALSO πŸ‘‘

a whole friend group honestly, go say hi 🫢

🌈 HISTORY 🌈

strnstr() originated in FreeBSD. her origin story, her lore, her main character energy 🌟


kisses bestie, this page came to you from Debian, dated October 11, 2001 β€” yes she's been serving since 2001 and she's still eating πŸ’…βœ¨ mwah πŸ’–

πŸ“¦ where this page comes from