• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php返回在字符串中查找到任何指定的字符之前的字符数函数strcspn()

php 搞代码 3年前 (2022-01-22) 20次浏览 已收录 0个评论

实例

输出在字符串 "Hello world!" 中找到字符 "w" 之前查找的字符数:

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

定义和用法

strcspn() 函数返回在找到任何指定的字符之前,在字符串查找的字符数(包括空格)。

提示: Use the strspn() function to the number of characters found in the string that contains only characters from a specified character list.

注释: This function is binary-safe.

语法

strcspn(string,char,start,length)
参数 描述
string 必需。规定要搜索的字符串。
char 必需。规定要查找的字符。
start 可选。规定开始查找的位置。
length 可选。规定字符串的长度(搜索多少字符)本文来源gao.dai.ma.com搞@代*码#网搞代gaodaima码

更多实例

实例 1

使用所有的参数来输出在字符串 "Hello world!" 中找到字符 "w" 之前查找的字符数:

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

函数功能:以str1为参照,比较字符串str2中的字符是否与str中某个字符相等(也就是检索str2中的字符是否在str1中存在),如果第一次发现相等,则停止并返回在str1中这个匹配相等的字符的索引值,失败则返回str1的长度。

返回说明:返回在str1中这个匹配相等的字符的索引值,是一个整数值。

其它说明:暂时无。

实例:

#include <string.h>#include <stdio.h>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;}

在VC++ 6.0编译运行:

以上就是php返回在字符串中查找到任何指定的字符之前的字符数函数strcspn()的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php返回在字符串中查找到任何指定的字符之前的字符数函数strcspn()

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址