以下是一段歌词,请从这段歌词中统计出朋友出现的次数。 “这些年一个人,风也过,雨也走,有过泪,有过错, 还记得坚持甚么,真爱过才会懂,会寂寞会回首,终有梦终有你在心中。 朋友一生一起走,那些日子不再
fun 0
package com.xs;
public class FriendCount {
public static void main(String[] args) {
String friend = "这些年一个人,风也过,雨也走,有过泪,有过错, 还记得坚持甚么,真爱过才会懂,会寂寞会回首,终有梦终有你在心中。 朋友一生一起走,那些日子不再有,一句话,一辈子,一生情,一杯酒。朋友不曾孤单过,一声朋友你会懂,还有伤,还有痛,还要走,还有我。";
String[] friends = new String[friend.length()+1];
int count = 0;
for (int i = 0; i <friend.length() ; i++) {
friends[i] = friend.charAt(i) + "";
}
for (int i = 0; i < friends.length; i++) {
if (i == friends.length -2){
break;
}
if (friends[i].equals("朋") && friends[i+1].equals("友")){
count++;
}
}
System.out.println("朋友出现的次数为:"+count);
}
}
fun 1
String text = “这些年一个人,风也过,雨也走,有过泪,有过错, 还记得坚持甚么,真爱过才会懂,会寂寞会回首,终有梦终有你在心中。 朋友一生一起走,那些日子不再有,一句话,一辈子,一生情,一杯酒。朋友不曾孤单过,一声朋友你会懂,还有伤,还有痛,还要走,还有我。”;
int oldLong,newLong,count;
oldLong = text.length();
text = text.replace("朋友","");
newLong = text.length();
count = (oldLong-newLong)/2;
System.out.println("朋友一共出现:"+count+"次");
fun 2
public static void main(String[] args) {
String str="这些年一个人,风也过,雨也走,有过泪,有过错, 还记得坚持甚么,真爱过才会懂,会寂寞会回首,终有梦终有你在心中。 朋友一生一起走,那些日子不再有,一句话,一辈子,一生情,一杯酒。朋友不曾孤单过,一声朋友你会懂,还有伤,还有痛,还要走,还有我。";
String[] str1=str.split("朋友");
int length = str1.length;
System.out.println(length-1);
}
fun 3
String str="这些年一个人,风也过,雨也走,有过泪,有过错,还记得坚持甚么,真爱过才会懂,会寂寞会回首,终有梦终有你在心中。朋友一生一起走,那些日子不再有,一句话,一辈子,一生情,一杯酒。朋友不曾孤单过,一声朋友你会懂,还有伤,还有痛,还要走,还有我。";
str.replaceAll(" ","");
System.out.println(str);
int index=str.indexOf("朋友"),count=0;
while(index!=-1){
count++;
str=str.substring(index+"朋友".length());
index=str.indexOf("朋友");
}
System.out.println(count);