• Java练习作业


    String str = "Nothing is impossible to a willing heart";

                       String str2 = "No cross, no crown.";

      1, 打印整个字符串去掉所有空格之后的长度

    public class Test4{
    	public static void main(String[] args) {
    		 String str = "Nothing is impossible to a willing heart";
             String str2 = "No cross, no crown.";
            System.out.println(str.replace(" ","").length());//用replace替换空格。然后在打印长度。
    		System.out.println(str2.replace(" ","").length());
    
    	   }
        }
    

    输出:

    2, 写代码找出字母"o","s"所在字符串str中第一次出现的索引位置, 找出字符串中最后一个"t"的索引位置, 并输出在控制台上

    public class Test4{
    	public static void main(String[] args) {
    		 String str = "Nothing is impossible to a willing heart";
             String str2 = "No cross, no crown.";
            System.out.println(str.indexOf("o"));
    		System.out.println(str.indexOf("s"));
    		System.out.println(str.lastIndexOf("t"));
    
    	   }
    

    输出:

     3, 写代码实现将str字符串用"空格"分割成数组, 并输出索引值为4的值

    public class Test4{
    	public static void main(String[] args) {
    		 String str = "Nothing is impossible to a willing heart";
             String str2 = "No cross, no crown.";
            System.out.println(str.split(" ")[4]);//转换完后就是一个数组,直接在数组去索引值就可以
             }
    	   }
       
    

    输出:

      4, 写代码实现将str字符串中所有的"i"替换成"I"

    public class Test4{
    	public static void main(String[] args) {
    		 String str = "Nothing is impossible to a willing heart";
             String str2 = "No cross, no crown.";
    		 System.out.println(str.replace("i","I"));     
    	   }
    }
       
    

    输出:

     5, 编写代码从str字符串中取每个单词的首字母打印在控制台上

    public class Test4{
    	public static void main(String[] args) {
    		 String str = "Nothing is impossible to a willing heart";
             String str2 = "No cross, no crown.";
    			 String[] strArray = str.split(" ");//把str分解为用空格隔开的几个数字(每个单词是一个数组)
                for (int i = 0; i < strArray.length; i++) {
                 System.out.println(strArray[i].charAt(0));//获取新数组索引为0的值。
                }
    	   }
    }
    

    输出:

    6, 在不使用第三个变量的情况下互换str和str2的值

    public class Test4{
    	public static void main(String[] args) {
    		 String str = "Nothing is impossible to a willing heart";
             String str2 = "No cross, no crown.";
    			 System.out.println("str=" +str . replace(str,str2)) ;
                System.out.println("str2=" +str2 . replace(str2,str)) ;
             }
    }   
    

    输出:

    第二种方法

    public class Test4{
    	public static void main(String[] args) {
    		 String str = "Nothing is impossible to a willing heart";
             String str2 = "No cross, no crown.";
    			str += str2;//str和str2拼接    str=str+str2
            str2 = str.substring(0, str.length() - str2.length());//截取str从0到str长度减str2的长度相当于"Nothing is impossible to a willing heart No cross, no crown."-" No cross, no crown."
            str = str.substring(str2.length());//截取从str2长度(包括)开始相当于从" No cross, no crown."开始到最后
            System.out.println("str=" + str);
            System.out.println("str2=" + str2);
    	}
    }   
    

      

  • 相关阅读:
    SpingBoot myBatis neo4j整合项目案例
    GCC 优化选项 -O1 -O2 -O3 -OS 优先级,-FOMIT-FRAME-POINTER(O3的优化很小,只增加了几条优化而已)
    睡个好觉的 12 条军规(坚持固定睡眠时间表,这一条最重要)
    恐怕你确定自己喜欢做什么(如果一件事能让你沉浸其中、安住当下,过后又不令你后悔,那它就是你喜欢的事:时间就应该拿来赚钱或提升赚钱的能力)
    专家解读:缺芯少人的中国集成电路,亟待打破高校学科壁垒
    你们一定要搞清楚被迫加班和自己弄的差别(被动的人得关节炎,主动的人身体更棒了)
    Linux命令排查线上问题常用的几个
    NFS (网络文件系统)
    查看JVM运行时堆内存
    SQL查询速度
  • 原文地址:https://www.cnblogs.com/xiaohaihuaihuai/p/7840925.html
Copyright © 2020-2023  润新知