Go to main content

man pages section 3: Basic Library Functions

Exit Print View

Updated: Wednesday, July 27, 2022
 
 

strrchr(3C)

Name

strchr, strrchr, strchrnul, strcspn, strspn, strpbrk, strstr, strnstr, strcasestr - string search operations

Synopsis

#include <string.h>

char *strchr(const char *s, int c);
char *strrchr(const char *s, int c);
char *strchrnul(const char *s, int c);
size_t strcspn(const char *s1, const char *s2);
size_t strspn(const char *s1, const char *s2);
char *strpbrk(const char *s1, const char *s2);
char *strstr(const char *s1, const char *s2);
char *strnstr(const char *s1, const char *s2, size_t n);
char *strcasestr(const char *s1, const char *s2);

ISO C++

#include <string.h>

const char *strchr(const char *s, int c);
const char *strpbrk(const char *s1, const char *s2);
const char *strrchr(const char *s, int c);
const char *strstr(const char *s1, const char *s2);
#include <cstring>

char *std::strchr(char *s, int c);
char *std::strpbrk(char *s1, const char *s2);
char *std::strrchr(char *s, int c);
char *std::strstr(char *s1, const char *s2);

Description

These functions perform various operations on strings (arrays of characters), which are given as arguments s, s1, and s2 that point to strings. Depending on the function, strings may be either terminated by a null character or consist of a length of n bytes. They are designed for use with encodings representing each character as a single byte, and all character counts are measured in individual bytes, even if a string with multibyte characters is used. They do not check for null pointers, and programs may crash if passing null or otherwise invalid pointers to these functions, or specifying sizes larger than the memory allocation in use for the string. Use of adi(7) may help in detecting out-of-bounds access or invalid pointer usage in code.

strchr(), strrchr(), strchrnul()

The strchr() function returns a pointer to the first occurrence of c (converted to a char) in string s, or a null pointer if c does not occur in the string.

The strrchr() function returns a pointer to the last occurrence of c (converted to a char) in string s, or a null pointer if c does not occur in the string.

The null character terminating a string is considered to be part of the string, and a pointer to it will be returned if c is 0.

The strchrnul() function is similar to strchr() except that if c is not found in s, it returns a pointer to the null byte at the end of s, rather than NULL.

These functions only work with single byte characters.

strcspn(), strspn()

The strcspn() function returns the length of the initial segment of string s1 that consists entirely of bytes not from string s2. The strspn() function returns the length of the initial segment of string s1 that consists entirely of bytes from string s2.

strpbrk()

The strpbrk() function returns a pointer to the first occurrence in string s1 of any byte from string s2, or a null pointer if no byte from s2 exists in s1.

strstr(), strnstr(), strcasestr()

The strstr() function locates the first occurrence of the string s2 (excluding the terminating null character) in string s1 and returns a pointer to the located string, or a null pointer if the string is not found. If s2 points to a string with zero length (that is, the string ""), the function returns s1.

The strnstr() function locates the first occurrence of the null-terminated string s2 in the string s1, where not more than n bytes are searched. Characters that appear after a ‘\0’ character are not searched.

The strcasestr() function is similar to strstr(), but ignores differences in case when comparing lowercase and uppercase characters, using the current locale of the process to determine the case of the characters.

Attributes

See attributes(7) for descriptions of the following attributes:

ATTRIBUTE TYPE
ATTRIBUTE VALUE
Interface Stability
Committed
MT-Level
Async-Signal-Safe
Standard
See below

Standard

See standards(7) for descriptions of the following standards:

INTERFACES
APPLICABLE STANDARDS
strchr(), strcspn(), strpbrk(), strrchr(), strspn(), strstr()
  • C89 through C11,
  • POSIX.1-1990 through 2008,
  • SUS through SUSv4,
  • XPG1 through XPG7
strcasestr(), strchrnul(), strnstr()
None

See Also

memchr(3C), setlocale(3C), string(3C), strtok(3C), wcschr(3C), wmemchr(3C), attributes(7), standards(7)

History

The strcasestr(), strchrnul(), and strnstr() functions were added to Oracle Solaris in the Solaris 11.0.0 release.

The strchr(), strcspn(), strpbrk(), strrchr(), strspn(), and strstr() functions have been included in all Sun and Oracle releases of Solaris.