---
name: "strsub"
section: "3pub"
platform: "linux"
package: "publib-dev"
version: "0.40-4"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/strsub"
---

# strsub(3pub)

substitute first occurrence of pattern with another string

## NAME

strsub - substitute first occurrence of pattern with another string

## SYNOPSIS

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

## DESCRIPTION

*strsub* finds the first occurrence of the pattern *pat* in the string *str* (using a method similar to [*strstr*(3)](/strstr/3.md), i.e., no regular expressions), and replaces it with *sub*.  If *pat* does not occur in *str*, no substitution is made.

Of course, if *sub* is an empty string, the pattern is deleted from the string.

## RETURN VALUE

*strsub* returns a pointer to the first character after the substitution, or NULL if no substitution was made.

## EXAMPLE

To substitute up to two occurrences of "foo" with "bar" in a line, one might do the following.

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

## SEE ALSO

publib(3), [strstr(3)](/strstr/3.md), strgsub(3)

## AUTHOR

Lars Wirzenius (lars.wirzenius@helsinki.fi)
