实验三 String类的应用
一、 实验目的
(1) 掌握类String类的使用;
(2) 学会使用JDK帮助文档;
二、 实验内容
1.已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码、结果截图。)
① 统计该字符串中字母s出现的次数。
② 统计该字符串中子串“is”出现的次数。
③ 统计该字符串中单词“is”出现的次数。
④ 实现该字符串的倒序输出。
实验代码:
packagecom.company;public classMain {public static voidmain(String[] args){int count=0,count1=0,count2=0;
String ch="this is a test of java";char[] c=ch.toCharArray();for(int i=0;i
count++;
}if(c[i]=='i'&&c[i+1]=='s'){
count1++;if(c[i-1]==' '&&c[i+2]==' '){
count2++;
}
}
}
System.out.println("字符串中字母s出现的次数:"+count);
System.out.println("字符串中字串“is”出现