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'.
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.
strchr
function finds the first occurrence of the character
c (converted to a char
) 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", '?') => NULL
The 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.
index
is another name for strchr
; they are exactly the same.
strrchr
is like strchr
, 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"
rindex
is another name for strrchr
; they are exactly the same.
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"
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.
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") => 5
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,.;!?") => 5
strpbrk
("string pointer break") function is related to
strcspn
, 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"
Go to the first, previous, next, last section, table of contents.