This section describes library functions which perform various kinds of searching operations on strings and arrays. These functions are declared in the header file string.h.
This function finds the first occurrence of the byte c (converted to an
unsigned char
) in the initial size bytes of the object beginning at block. The return value is a pointer to the located byte, or a null pointer if no match was found.
This function finds the first occurrence of the wide character wc in the initial size wide characters of the object beginning at block. The return value is a pointer to the located wide character, or a null pointer if no match was found.
Often the
memchr
function is used with the knowledge that the byte c is available in the memory block specified by the parameters. But this means that the size parameter is not really needed and that the tests performed with it at runtime (to check whether the end of the block is reached) are not needed.The
rawmemchr
function exists for just this situation which is surprisingly frequent. The interface is similar tomemchr
except that the size parameter is missing. The function will look beyond the end of the block pointed to by block in case the programmer made an error in assuming that the byte c is present in the block. In this case the result is unspecified. Otherwise the return value is a pointer to the located byte.This function is of special interest when looking for the end of a string. Since all strings are terminated by a null byte a call like
rawmemchr (str, '\0')will never go beyond the end of the string.
This function is a GNU extension.
The function
memrchr
is likememchr
, except that it searches backwards from the end of the block defined by block and size (instead of forwards from the front).This function is a GNU extension.
The
strchr
function finds the first occurrence of the character c (converted to achar
) in the null-terminated string beginning at string. The return value is a pointer to the located character, or a null pointer if no match was found.For example,
strchr ("hello, world", 'l') ⇒ "llo, world" strchr ("hello, world", '?') ⇒ NULLThe terminating null character is considered to be part of the string, so you can use this function get a pointer to the end of a string by specifying a null character as the value of the c argument.
When
strchr
returns a null pointer, it does not let you know the position of the terminating null character it has found. If you need that information, it is better (but less portable) to usestrchrnul
than to search for it a second time.
The
wcschr
function finds the first occurrence of the wide character wc in the null-terminated wide character string beginning at wstring. The return value is a pointer to the located wide character, or a null pointer if no match was found.The terminating null character is considered to be part of the wide character string, so you can use this function get a pointer to the end of a wide character string by specifying a null wude character as the value of the wc argument. It would be better (but less portable) to use
wcschrnul
in this case, though.
strchrnul
is the same asstrchr
except that if it does not find the character, it returns a pointer to string's terminating null character rather than a null pointer.This function is a GNU extension.
wcschrnul
is the same aswcschr
except that if it does not find the wide character, it returns a pointer to wide character string's terminating null wide character rather than a null pointer.This function is a GNU extension.
One useful, but unusual, use of the strchr
function is when one wants to have a pointer pointing to the NUL byte
terminating a string. This is often written in this way:
s += strlen (s);
This is almost optimal but the addition operation duplicated a bit of
the work already done in the strlen
function. A better solution
is this:
s = strchr (s, '\0');
There is no restriction on the second parameter of strchr
so it
could very well also be the NUL character. Those readers thinking very
hard about this might now point out that the strchr
function is
more expensive than the strlen
function since we have two abort
criteria. This is right. But in the GNU C Library the implementation of
strchr
is optimized in a special way so that strchr
actually is faster.
The function
strrchr
is likestrchr
, except that it searches backwards from the end of the string string (instead of forwards from the front).For example,
strrchr ("hello, world", 'l') ⇒ "ld"
The function
wcsrchr
is likewcschr
, except that it searches backwards from the end of the string wstring (instead of forwards from the front).
This is like
strchr
, except that it searches haystack for a substring needle rather than just a single character. It returns a pointer into the string haystack that is the first character of the substring, or a null pointer if no match was found. If needle is an empty string, the function returns haystack.For example,
strstr ("hello, world", "l") ⇒ "llo, world" strstr ("hello, world", "wo") ⇒ "world"
This is like
wcschr
, except that it searches haystack for a substring needle rather than just a single wide character. It returns a pointer into the string haystack that is the first wide character of the substring, or a null pointer if no match was found. If needle is an empty string, the function returns haystack.
wcswcs
is an deprecated alias forwcsstr
. This is the name originally used in the X/Open Portability Guide before the Amendment 1 to ISO C90 was published.
This is like
strstr
, except that it ignores case in searching for the substring. Likestrcasecmp
, it is locale dependent how uppercase and lowercase characters are related.For example,
strcasestr ("hello, world", "L") ⇒ "llo, world" strcasestr ("hello, World", "wo") ⇒ "World"
This is like
strstr
, but needle and haystack are byte arrays rather than null-terminated strings. needle-len is the length of needle and haystack-len is the length of haystack.This function is a GNU extension.
The
strspn
(“string span”) function returns the length of the initial substring of string that consists entirely of characters that are members of the set specified by the string skipset. The order of the characters in skipset is not important.For example,
strspn ("hello, world", "abcdefghijklmnopqrstuvwxyz") ⇒ 5Note that “character” is here used in the sense of byte. In a string using a multibyte character encoding (abstract) character consisting of more than one byte are not treated as an entity. Each byte is treated separately. The function is not locale-dependent.
The
wcsspn
(“wide character string span”) function returns the length of the initial substring of wstring that consists entirely of wide characters that are members of the set specified by the string skipset. The order of the wide characters in skipset is not important.
The
strcspn
(“string complement span”) function returns the length of the initial substring of string that consists entirely of characters that are not members of the set specified by the string stopset. (In other words, it returns the offset of the first character in string that is a member of the set stopset.)For example,
strcspn ("hello, world", " \t\n,.;!?") ⇒ 5Note that “character” is here used in the sense of byte. In a string using a multibyte character encoding (abstract) character consisting of more than one byte are not treated as an entity. Each byte is treated separately. The function is not locale-dependent.
The
wcscspn
(“wide character string complement span”) function returns the length of the initial substring of wstring that consists entirely of wide characters that are not members of the set specified by the string stopset. (In other words, it returns the offset of the first character in string that is a member of the set stopset.)
The
strpbrk
(“string pointer break”) function is related tostrcspn
, except that it returns a pointer to the first character in string that is a member of the set stopset instead of the length of the initial substring. It returns a null pointer if no such character from stopset is found.For example,
strpbrk ("hello, world", " \t\n,.;!?") ⇒ ", world"Note that “character” is here used in the sense of byte. In a string using a multibyte character encoding (abstract) character consisting of more than one byte are not treated as an entity. Each byte is treated separately. The function is not locale-dependent.
The
wcspbrk
(“wide character string pointer break”) function is related towcscspn
, except that it returns a pointer to the first wide character in wstring that is a member of the set stopset instead of the length of the initial substring. It returns a null pointer if no such character from stopset is found.