以下程序的功能是,使用String类中的indexOf()方法统计一个字符串在另一个字符串中的出现次数.

例如字符串"this"在字符串"thisismyfirstprogram.this…publicclassJava1{publicstaticvoidmain(Strin... 例如字符串"this"在字符串"this is my first program. this…

public class Java1
{
public staticvoid main(String[] args)
{
Stringstr1="this";
Stringstr2="this is my first program. this...";
intcount=0,len1,len2;
len1=str1.length();
len2=str2.length();
/**********************************/
for(inti=0;i?len2-len1;i++)
{
/**********************************/
intj=str2.?;
if(j==i)
/**********************************/
?;
}
System.out.println(count);

}
}
展开
 我来答
下周四到
推荐于2017-09-20 · TA获得超过271个赞
知道小有建树答主
回答量:340
采纳率:0%
帮助的人:177万
展开全部

public static void main(String[] args) {

  String str1="this";

      String str2="this is my first program. this...";

      int count=0,len1,len2;

      len1=str1.length();

      len2=str2.length();

   /**********************************/

      for(int i=0;i<len2-len1;i++)

      {

   /**********************************/

          int j=str2.indexOf(str1,i);

          if(j==i)

   /**********************************/

           count++;

      }

      System.out.println(count);


}

liuyang054
2015-06-04 · TA获得超过9093个赞
知道大有可为答主
回答量:5317
采纳率:78%
帮助的人:5432万
展开全部
public class IndexOfTest {

 public static void main(String[] args) {
  String a ="abcabc23423abc342334abc34534abc23423";
  String b="abc"; 
  System.out.println(getSubNum(a,b));
 }
 public static int getSubNum(String a,String b){
  int num=0;
  String str=a;
  int index=a.indexOf(b);
  while(index!=-1){
   num++;
   str=str.substring(index+b.length()-1);
   index=str.indexOf(b);
  }
  return num;
 }

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
jhjhy123
2015-06-04 · 超过67用户采纳过TA的回答
知道小有建树答主
回答量:212
采纳率:0%
帮助的人:66.6万
展开全部
什么问题?
追问
以下程序的功能是,使用String类中的indexOf()方法统计一个字符串在另一个字符串中的出现次数。例如字符串“this”在字符串“thisis my first program. this…”中出现了2次。请将程序补充完整。
注意:请勿改动程序已有内容,仅在?处填入适当的语句。
追答
package com.jbit.test1;

public class Java1 {
public static void main(String[] args) {
String str1 = "this";
String str2 = "this is my first program. this...";
int count = 0, len1, len2;
len1 = str1.length();
len2 = str2.length();
/**********************************/
for (int i = 0; i <= len2 - len1; i++) {
/**********************************/
int j = str2.indexOf("this");
if (j == i) {
/**********************************/
count++;
str2 = str2.substring(str1.length() + j);
i = 0;
len2 = str2.length();
}
}
System.out.println(count);

}
}

这样就可以了

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式