Example
Output The number of characters found before the character "w" is found inString"Hello world!":
Definition and usage
strcspn() function returns the number of characters (including spaces) searched in the string before any specified character is found.
Tips: Use the strspn() function to the number of characters found in the string that contains only characters from a specified character list.
Comments: This function is binary-safe.
Syntax
strcspn(string,char,start,length)
Parameters | Description |
string | Required. Specifies the string tosearchfor. |
char | Required. Specifies the characters to search for. |
start | Optional. Specifies the location to start the search. |
length | Optional. Specifies the length of the string (how many characters to search for). |
More examples
Example 1
Use all parameters to output the characters found in the string "Hello world!" w" Number of characters searched before:
Function function: Using str1 as a reference, compare whether the characters in the string str2 are equal to a character in str (that is, search whether the characters in str2 are in str1 exists), if equality is found for the first time, stop and return theindexvalue of the matching equal character in str1, or return the length of str1 if it fails.
Return description: Return the index value of the matching equal character in str1, which is an integer value.
Other instructions: None at the moment.
Example:
#include#include int main() { char *str1="aaaaakkkeeee"; char *str2="John,he like writing!"; int inttemp; inttemp=strcspn(str1,str2); //将str2中的每个字符均与str1中的匹配,如果这个字符在str1中出现过,则返回这个字符在str1中的索引值 printf("The first index of the character both in str1 and str2: %d ", inttemp); return 0; }
Compile and run in VC++ 6.0:
The above is the detailed content of The php function strcspn() returns the number of characters before any specified character is found in the string.. For more information, please follow other related articles on the PHP Chinese website!