char * strpbrk (const char *string, const char *stopset)
strpbrk
(“string pointer break”)
size_t strcspn (const char *string, const char *stopset)
strcspn
(“string complement span”)
size_t strspn (const char *string, const char *skipset)
strspn
(“string span”)
参考:http://www.gnu.org/software/libtool/manual/libc/Search-Functions.html
This section describes library functions which perform various kindsof searching operations on strings and arrays. These functions aredeclared in the header filestring.h.
This function finds the first occurrence of the byte c (convertedto an
unsigned char
) in the initialsize bytes of theobject beginning atblock. The return value is a pointer to thelocated byte, or a null pointer if no match was found.
This function finds the first occurrence of the wide character wcin the initialsize wide characters of the object beginning atblock. The return value is a pointer to the located widecharacter, or a null pointer if no match was found.
Often the
memchr
function is used with the knowledge that thebytec is available in the memory block specified by theparameters. But this means that thesize parameter is not reallyneeded and that the tests performed with it at runtime (to check whetherthe end of the block is reached) are not needed.The
rawmemchr
function exists for just this situation which issurprisingly frequent. The interface is similar tomemchr
exceptthat thesize parameter is missing. The function will look beyondthe end of the block pointed to byblock in case the programmermade an error in assuming that the bytec is present in the block. In this case the result is unspecified. Otherwise the return value is apointer to the located byte.This function is of special interest when looking for the end of astring. 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 searchesbackwards from the end of the block defined byblock andsize(instead of forwards from the front).This function is a GNU extension.
The
strchr
function finds the first occurrence of the characterc (converted to achar
) in the null-terminated stringbeginning atstring. The return value is a pointer to the locatedcharacter, 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 byspecifying a null character as the value of thec argument. Itwould be better (but less portable) to use
strchrnul
in thiscase, though.
The
wcschr
function finds the first occurrence of the widecharacterwc in the null-terminated wide character stringbeginning atwstring. The return value is a pointer to thelocated wide character, or a null pointer if no match was found.The terminating null character is considered to be part of the widecharacter string, so you can use this function get a pointer to the endof a wide character string by specifying a null wude character as thevalue of thewc argument. It would be better (but less portable)to use
wcschrnul
in this case, though.
strchrnul
is the same asstrchr
except that if it doesnot find the character, it returns a pointer to string's terminatingnull character rather than a null pointer.This function is a GNU extension.
wcschrnul
is the same aswcschr
except that if it does notfind the wide character, it returns a pointer to wide character string'sterminating 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 byteterminating a string. This is often written in this way:
s += strlen (s);
This is almost optimal but the addition operation duplicated a bit ofthe work already done in thestrlen
function. A better solutionis this:
s = strchr (s, '\0');
There is no restriction on the second parameter of strchr
so itcould very well also be the NUL character. Those readers thinking veryhard about this might now point out that thestrchr
function ismore expensive than thestrlen
function since we have two abortcriteria. This is right. But in the GNU C library the implementation ofstrchr
is optimized in a special way so thatstrchr
actually is faster.
The function
strrchr
is likestrchr
, except that it searchesbackwards from the end of the stringstring (instead of forwardsfrom the front).For example,
strrchr ("hello, world", 'l') ⇒ "ld"
The function
wcsrchr
is likewcschr
, except that it searchesbackwards from the end of the stringwstring (instead of forwardsfrom the front).
This is like
strchr
, except that it searches haystack for asubstringneedle rather than just a single character. Itreturns a pointer into the stringhaystack that is the firstcharacter of the substring, or a null pointer if no match was found. Ifneedle is an empty string, the function returnshaystack.For example,
strstr ("hello, world", "l") ⇒ "llo, world" strstr ("hello, world", "wo") ⇒ "world"
This is like
wcschr
, except that it searches haystack for asubstringneedle rather than just a single wide character. Itreturns a pointer into the stringhaystack that is the first widecharacter of the substring, or a null pointer if no match was found. Ifneedle is an empty string, the function returnshaystack.
wcswcs
is an deprecated alias forwcsstr
. This is thename originally used in the X/Open Portability Guide before theAmendment 1 to ISO C90 was published.
This is like
strstr
, except that it ignores case in searching forthe substring. Likestrcasecmp
, it is locale dependent howuppercase and lowercase characters are related.For example,
strcasestr ("hello, world", "L") ⇒ "llo, world" strcasestr ("hello, World", "wo") ⇒ "World"
const void *needle, size_t needle-len)
This is like
strstr
, but needle and haystack are bytearrays rather than null-terminated strings.needle-len is thelength ofneedle and haystack-len is the length ofhaystack.This function is a GNU extension.
The
strspn
(“string span”) function returns the length of theinitial substring ofstring that consists entirely of characters thatare members of the set specified by the stringskipset. The orderof the characters inskipset is not important.For example,
strspn ("hello, world", "abcdefghijklmnopqrstuvwxyz") ⇒ 5Note that “character” is here used in the sense of byte. In a stringusing a multibyte character encoding (abstract) character consisting ofmore than one byte are not treated as an entity. Each byte is treatedseparately. The function is not locale-dependent.
The
wcsspn
(“wide character string span”) function returns thelength of the initial substring ofwstring that consists entirelyof wide characters that are members of the set specified by the stringskipset. The order of the wide characters inskipset is notimportant.
The
strcspn
(“string complement span”) function returns the lengthof the initial substring ofstring that consists entirely of charactersthat arenot members of the set specified by the stringstopset. (In other words, it returns the offset of the first character instringthat is a member of the setstopset.)For example,
strcspn ("hello, world", " \t\n,.;!?") ⇒ 5Note that “character” is here used in the sense of byte. In a stringusing a multibyte character encoding (abstract) character consisting ofmore than one byte are not treated as an entity. Each byte is treatedseparately. The function is not locale-dependent.
The
wcscspn
(“wide character string complement span”) functionreturns the length of the initial substring ofwstring thatconsists entirely of wide characters that arenot members of theset specified by the stringstopset. (In other words, it returnsthe offset of the first character instring that is a member ofthe setstopset.)
The
strpbrk
(“string pointer break”) function is related tostrcspn
, except that it returns a pointer to the first characterinstring that is a member of the setstopset instead of thelength of the initial substring. It returns a null pointer if no suchcharacter fromstopset is found.For example,
strpbrk ("hello, world", " \t\n,.;!?") ⇒ ", world"Note that “character” is here used in the sense of byte. In a stringusing a multibyte character encoding (abstract) character consisting ofmore than one byte are not treated as an entity. Each byte is treatedseparately. The function is not locale-dependent.
The
wcspbrk
(“wide character string pointer break”) function isrelated towcscspn
, except that it returns a pointer to the firstwide character inwstring that is a member of the setstopset instead of the length of the initial substring. Itreturns a null pointer if no such character fromstopset is found.
5.7.1 Compatibility String Search Functions
index
is another name forstrchr
; they are exactly the same. New code should always usestrchr
since this name is defined inISO C whileindex
is a BSD invention which never was availableon System V derived systems.
rindex
is another name forstrrchr
; they are exactly the same. New code should always usestrrchr
since this name is defined inISO C whilerindex
is a BSD invention which never was availableon System V derived systems.