当前位置: 代码迷 >> Java相关 >> 字符串问题
  详细解决方案

字符串问题

热度:28   发布时间:2007-05-08 22:38:58.0
字符串问题
编写一个用string类中的的indexOf方法,确定一个字符串在另一个字符串中出现次数的应用程序。。
我是这样写的:
public class Test
{
public static void main(String[] args)
{
String s1="i";
String s2="i an a i an a i";
int d=0;
int count=0;
while(i=-1)
{
public int indexOf(String s1,int d)
{
count=count+1;
}
System.out.println(count);
}
}
}

帮忙修改下啊!

谢谢。。。
搜索更多相关的解决方案: 字符  

----------------解决方案--------------------------------------------------------

[CODE]public class Test
{

public static int count(String s1, String s2)
{
int number = 0;
for (int i = 0; i < s2.length(); i++)
{
int lengths2 = s2.length();
int num = s2.indexOf(s1);

if(num >= 0)
{
number++;
}

s2 = s2.substring(num+1, lengths2);
}
return number;
}


public static void main(String[] args)
{
String s1="i";
String s2="i an a i an a i";

System.out.println (count(s1, s2));
}
}

[/CODE]

结果是3 ,对吗?


----------------解决方案--------------------------------------------------------

大哥,好像有点问题,就是你在后面连续加几个i i i i i的时候问题就出来了。

[此贴子已经被作者于2007-5-13 5:58:16编辑过]


----------------解决方案--------------------------------------------------------

或许是我对这个题目理解的不深刻。。。。
我做了一种方法。。。
public class Test
{
public static void main(String[] args)
{
String s1="i";
String s2="i an a i an a i i i i";
int number = 0;
int num;
while(s2.indexOf(s1)!=-1)
{

int lengths2 = s2.length();
num = s2.indexOf(s1);
number++;
System.out.println (num);
System.out.println (number);
s2 = s2.substring(num+1, lengths2);
}


}
}


----------------解决方案--------------------------------------------------------
  相关解决方案