PHP returns the number of characters before any specified character is found in a string function strcspn ()

Source: Internet
Author: User

Instance

The number of characters that the output looks for before the character "W" is found in the string "Hello world!":

<?phpecho strcspn ("Hello world!", "w");? >

Definition and usage

The STRCSPN () function returns the number of characters (including spaces) that are searched for in a string before any specified characters are found.

Tip: Use the strspn () function to the number of characters found in the string that contains only characters from a specifi Ed character List.

Note: This function is Binary-safe.

Grammar

STRCSPN (String,char,start,length)
parameters description
string required. Specifies the string to search for.
char start optional. Specifies where to start the search.
length optional. Specifies the length of the string (how many characters to search).

More examples

Example 1

Use all the parameters to output the number of characters found before the character "W" in the string "Hello world!":

<?phpecho strcspn ("Hello world!", "W", 0,6); The start position is 0 and the length of the search string is 6.? >

function function: Compare the characters in string str2 with a word typeface in Str (that is, whether the characters in the str2 are present in str1), and then stop and return the index value of this matching character in str1 if the first occurrence of the str1 is the same as the reference. Failure returns the length of the str1.

Return Description: Returns the index value of this match-equal character in str1, which is an integer value.

Other instructions: not currently.

Instance:

#include <string.h> #include <stdio.h>int main () {    char *str1= "aaaaakkkeeee";      Char *str2= "john,he like writing!";     int inttemp;    INTTEMP=STRCSPN (STR1,STR2);   Nonspacing each word in str2 to match in Str1, if the character appears in str1, returns the index value of this character in str1     printf ("The first index of the character both in str1 and STR2:%d  ", inttemp);    return 0;}

In VC + + 6.0 compile run:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.