The php function strcspn() returns the number of characters before any specified character is found in the string.

黄舟
Release: 2023-03-17 06:22:02
Original
2676 people have browsed it

Example

Output The number of characters found before the character "w" is found inString"Hello world!":

Copy after login

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)
Copy after login
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:

Copy after login

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; }
Copy after login

Compile and run in VC++ 6.0:

The php function strcspn() returns the number of characters before any specified character is found in the string.

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!