package com.company.string;
import java.util.Arrays;
/*
* reverse string
*/
public class Reverse {
public static void main(String[] args) {
String str = "新中国好不好打赛达大道";
Reverse re=new Reverse();
String s=re.reverse(str);
System.out.println(s);
int i=re.count(str, "新");
System.out.println("次数是"+i);
}
public int count(String str,String target){
int count=0;
int fromIndex=0;
}
return count;
}
}
import java.util.Arrays;
/*
* reverse string
*/
public class Reverse {
public static void main(String[] args) {
String str = "新中国好不好打赛达大道";
Reverse re=new Reverse();
String s=re.reverse(str);
System.out.println(s);
int i=re.count(str, "新");
System.out.println("次数是"+i);
}
public int count(String str,String target){
int count=0;
int fromIndex=0;
while((fromIndex=str.indexOf(target, fromIndex))!=-1){//布尔函数-如果带有目标子串存在则返回非-1的下标
//从找到的地方开始计算下标
count++;
fromIndex+=target.length();}
return count;
}
}
本文介绍了一个简单的Java程序,该程序能够实现字符串的反转功能,并提供了一个统计特定字符出现次数的方法。通过实例演示了如何使用这些功能。
2374

被折叠的 条评论
为什么被折叠?



