实验三 String类的应用
实验目的
掌握类String类的使用;
学会使用JDK帮助文档;
实验内容
1.已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码、结果截图。)
- 统计该字符串中字母s出现的次数。
- 统计该字符串中子串“is”出现的次数。
- 统计该字符串中单词“is”出现的次数。
- 实现该字符串的倒序输出。
package shiyan; public class Test { public static void main(String[] args) { String str="This is a test of java"; int k=0; for(int i=0;i<str.length();i++) { if(‘s‘==str.charat(i)) {//找出指定字符 k++; } } System.out.println(k); } }
package shiyan; public class Test4 { public static void main(String[] args) { String str="This is a test of java"; StringBuffer sb = new StringBuffer(str); System.out.println(sb.reverse().toString()); } }
package shiyan; public class Test3 { public static void main(String[] args) { String str="This is a test of java"; int count=0; String[] s=str.split(" "); for(String e:s) { if(e.equals("is")) { count++; } } System.out.println(count); } }