第三次实验

 

 

实验三 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);
    }
    

}

 

 

分享图片

 

 

package shiyan;

public class Test4 {
    public static void main(String[] args) {
        String str="This is a test of java";

//https://blog.csdn.net/Mo_KaIseR/article/details/52473432参考链接

    StringBuffer sb = new StringBuffer(str);
    System.out.println(sb.reverse().toString());
}
}

分享图片

相关文章

HashMap是Java中最常用的集合类框架,也是Java语言中非常典型...
在EffectiveJava中的第 36条中建议 用 EnumSet 替代位字段,...
介绍 注解是JDK1.5版本开始引入的一个特性,用于对代码进行说...
介绍 LinkedList同时实现了List接口和Deque接口,也就是说它...
介绍 TreeSet和TreeMap在Java里有着相同的实现,前者仅仅是对...
HashMap为什么线程不安全 put的不安全 由于多线程对HashMap进...