STRSUB(3pub)

✨ NAME ✨

okay so bestie, meet strsub β€” it substitutes the first occurrence of a pattern with another string πŸ’…


🌸 SYNOPSIS 🌸

#include <publib.h>
char *strsub(char *str, const char *pat, const char *sub);

πŸ¦‹ DESCRIPTION πŸ¦‹

So here's the tea β˜•: strsub goes looking for the first time the pattern pat shows up inside the string str, and it does that using a method similar to strstr(3) β€” which is a fancy way of saying no regular expressions, bestie, it's just plain matching πŸ™ˆ β€” and then it swaps that match out for sub.

And if pat literally never shows up in str? Then nothing happens. No substitution, no drama, we move on πŸ’€.

Oh and of course, if sub is an empty string, the pattern just gets deleted from the string. Poof. Gone. It's giving vanishing act ✨.


πŸ’– RETURN VALUE πŸ’–

strsub hands you back a pointer to the first character after the substitution 🫢 β€” or NULL if no substitution was made, because sometimes the answer is just no 😭.


πŸŽ€ EXAMPLE πŸŽ€

Say you wanna substitute up to two occurrences of "foo" with "bar" in a line. Iconic behaviour, honestly. You'd do:

p = strsub(line, "foo", "bar");
if (p != NULL)
     strsub(line, "foo", "bar");

See the move? First call does the swap, and if it actually worked (p != NULL) you go again for round two πŸ‘‘.


🌈 SEE ALSO 🌈


⭐ AUTHOR ⭐

Lars Wirzenius (lars.wirzenius@helsinki.fi) β€” the main character who wrote this 🫑


okay that's the whole page bestie, no cap πŸ’œ this lil gem lives in the Publib C Programmer's Manual, page STRSUB(3pub), so go forth and substitute responsibly 🌸✨

πŸ“¦ where this page comes from