java题目~~~需要你来编!!
怎样编写一个方法计算字符串中给定字符的出现次数!!!
大家看看哈!!!
=待你的回复!!
class Findstring
{
/** * @param args
*/
public static void main(String[] args)
{
String str = "Afafafafafafafaf";
int counts = 0;
String ads ;
ads = str.substring(0,2);
// TODO Auto-generated method stub
for (int i=0;i<str.length()-1;i++)
{
if (str.substring(i,i+2)=="af")
{counts++;
System.out.println(str.substring(i,i+2));}
}
System.out.println(counts);
System.out.println(ads);
System.out.println(str.length());
}
}
大家看看为什么for语句没执行~!
counts返回值为0!
按道理不会啊!!