定义函数findstr,实现在字符串str中查找是否存在子字符串str.substrr

统计子字符串substr在字符串str中出现的次数。_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
统计子字符串substr在字符串str中出现的次数。
||暂无简介
在校大学生,质量管理内审员|
总评分4.0|
浏览量552681
阅读已结束,如果下载本文需要使用0下载券
想免费下载更多文档?
你可能喜欢[代码全屏查看]-在字符串ss中查找子串subsr的个数,并输出子串第一次出现的位置
[1].[代码] [Python]代码
#coding= utf-8
def findStr(ss, substr):
if ss.find(substr) == -1:
print "not fund"
index = ss.find(substr)
res.append(index)
nlen = len(substr)
ss = ss[index+nlen:]
while ss != "":
index = ss.find(substr)
if index == -1:
return len(res),res[0]
res.append(index)
ss = ss[index+nlen:]
return len(res),res[0]
if __name__ == "__main__":
print findStr("fghabcjkabcabcghjabc", 'abc')
#res中的index对应的是每个ss[index+nlen:]中'abc'所在的位置,不是ss中的位置,只有第一个才是ss中出现的位置PHP基础教程 列表
相关参考课程PHP 字符串替换 substr_replace 与 str_replace 函数PHP 字符串替换
用于从字符串中替换指定字符串。
相关函数如下:
:把字符串的一部分替换为另一个字符串
:使用一个字符串替换字符串中的另一些字符
substr_replace() 函数用于把字符串的一部分替换为另一个字符串,返回混合类型。
mix substr_replace ( mixed string, string replacement, int start [, int length] )
参数说明如下:
要处理的字符串
replacement
要插入的字符串
字符串开始位置,起始位置为 0 ,为负则从字符串结尾的指定位置开始
可选,字符串返回的长度,默认是直到字符串的结尾,为负则从字符串末端返回
echo substr_replace('abcdef', '###', 1); //输出 a###
echo substr_replace('abcdef', '###', 1, 2); //输出 a###def
echo substr_replace('abcdef', '###', -3, 2); //输出 abc###f
echo substr_replace('abcdef', '###', 1, -2); //输出 a###ef
如果 start 是负数且 length 小于等于 start ,则 length 为 0。
str_replace() 函数使用一个字符串替换字符串中的另一些字符,返回混合类型。
mixed str_replace( mixed search, mixed replace, mixed string [, int &count] )
参数说明如下:
要查找(被替换)的字符串
要替换 search 的字符串
要处理的字符串
可选,一个对替换计数的变量
echo str_replace(&world&,&earth&,&Hello world!&);
//输出 Hello earth!
//替换多个,且第二个参数为空字符
echo str_replace(&o&,&&,&Hello world!&);
//输出 Hell wrld!
//使用数组
$arr = array(&e&, &o&);
$arr2 = array(&x&, &y&);
echo str_replace($arr, $arr2, &Hello World of PHP&, $i); //输出 Hxlly Wyrld yf PHP
该函数与 substr_replace() 不同之处是满足条件的都进行替换
该函数对大小写敏感。如需进行大小写不敏感的查找替换,请使用 str_ireplace()
本章节内容共分 10 部分:1.
PHP 字符串替换 substr_replace 与 str_replace 函数8.
<(我爱开发网) — 提供最好的 、、、 及题目:下面的函数统计子字符串substr在字符串str中出现的次数,如果substr在str中不出现, 则返回值0。_百度知道
题目:下面的函数统计子字符串substr在字符串str中出现的次数,如果substr在str中不出现, 则返回值0。
编译可以通过:样张;cout&&&lt。请完成该函数;s& }
#include &&cout&&#92?*substr, char *str){ *题目;&#92;str_count(void main(void){ char ss[100];**********,!=str[k])
break.getline(n&#39;0&#39?*str)输出结果见图;int str_count(char *&k++;}谁可以帮我看一下;&母串=&& char *s=&quot??ABCDabcd1234+-*&#47,c;using namespace std,j;;iostream&; for(i=j=c=0;Aa1 ABCDabcd&quot: int,则返回值0;)&
if(str[i]==&#39;&&#47;cstring&gt?**********&#47;子串在母串中共出现&}
if(substr[j];&次&
}j=0,k;**********Program**********&#47,k=i;
ABCDabcd1234+-*&#47,j++){
if(substr[j]==&#39;。格式;&&)
{ c++;&quot?:下面的函数统计子字符串substr在字符串str中出现的次数?E&lt,99).JPG-------------------------------------------------------*&#47,s)&#include &lt,如果substr在str中不出现;}&#47;XYZxyz&#47;i++){
if(substr[j]==str[i])
for(j=0?str_count(char&#47
不要写个新的给我,我只是想知道自己错在哪里
提问者采纳
p = strstr(p;&#92;
if(p;0&#39;int str_count(char *
for(sum=0,substr)!;结尾,p=str,
len = strlen(substr);!#include &lt.h&
break, char *str){
int sum!=NULL)
return -1,输入的字符串必须以&#39楼主注意使用下面的函数时
提问者评价
是我大意了吧‘、0’写成‘、n’了
其他类似问题
为您推荐:
substr的相关知识
其他1条回答
华为机试题吗?为什么不用库函数strcmp/strncmp比较字符串呢?
不知道,就是考试题
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁

我要回帖

更多关于 php strsubstr 的文章

 

随机推荐