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);
	}
}   

  

转载于:https://www.cnblogs.com/xiaohaihuaihuai/p/7840925.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值