The GNU C Library - Search Functions

Node: Search Functions Next: Finding Tokens in a String Prev: Collation Functions Up: String and Array Utilities

Search Functions

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'.

Function void * memchr (const void *block, int c, size_t size)
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.

Function char * strchr (const char *string, int c)
The 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.

Function char * index (const char *string, int c)
index is another name for strchr ; they are exactly the same.

Function char * strrchr (const char *string, int c)
The function 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"

Function char * rindex (const char *string, int c)
rindex is another name for strrchr ; they are exactly the same.

Function char * strstr (const char *haystack, const char *needle)
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"

Function void * memmem (const void *needle, size_t needle-len,
const void *haystack, size_t haystack-len)
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.

Function size_t strspn (const char *string, const char *skipset)
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")
	    => 5

Function size_t strcspn (const char *string, const char *stopset)
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,.;!?")
	    => 5

Function char * strpbrk (const char *string, const char *stopset)
The 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"


Next: Finding Tokens in a String Up: String and Array Utilities