4-23 String与StringBuffer的练习笔记

文章目录

package LXT;
/*
 *  ATM机取款案例,银行输入密码不能
          超过5次,超过5次账号锁定,密码匹
           配成功显示登录成功!

 */
import java.util.Scanner;

public class lxt1 {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		String password = "12345";
		System.out.println("********欢迎来到中国银行********");
		/*
		 *      i=0  剩余4次
		 *      i=1 剩余3次
		 *      i=2 剩余2次
		 *      i=3 剩余1次
		 *      i=4 剩余0次
		 */
		for (int i = 0; i < 5; i++) {
			System.out.println("请输入密码:");
			String pwd = input.next();
			if (pwd.equals(password)) {
				System.out.println("登录成功!");
				break;
			} else {
				if (i==4) {
					System.out.println("输入密码错误超过5次账户锁定");
				} else {
					System.out.println("您还有" + (4-i) + "次机会");
				}
			}
		}
	}
}
package LXT;
/*
 *    把字符串的首字母转大写,其他字母转小写,同时将每个字符使用_下划线隔开
 *    原字符串:youAreWelcome
       转换后字符串:Y_o_u_a_r_e_w_e_l_c_o_m_e

 */
public class lxt2 {
	public static void main(String[] args) {
	//1.获取到首字母,将首字母转换大写
	//2.获取到其他字母,其他字母转小写
	//3.拼接两个字符串
	System.out.println(String.join("_",String.valueOf("youAreWelcome".charAt(0)).toUpperCase().concat("youAreWelcome".substring(1).toLowerCase()).split("")));
	}
}
package LXT;
/*
 *            字符串倒置
 	        如键盘录入 “我爱你”		
 	         输出结果:  "你爱我”

 *       0 1 2       2 1 0
 *                    我  爱 你                 你  爱  我
 */
public class lxt3 {
	public static void main(String[] args) {
		String z = "我爱你";
		
		String result = "";
		for (int i = z.length()-1; i >= 0; i--) {
			result += z.charAt(i);
		}
		System.out.println(result);
	}
}
package LXT;
/*
 *                           我爱中国,你是我的中国,我的中国有你
 */
import java.util.Scanner;

public class lxt4 {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入字符串:");
		String line1 = input.next();
		System.out.println("请输入要找的字符串:");
		String line2 = input.next();
		int count = getLine2InLine1Count(line2,line1);
		System.out.println("中国个数为:  " + count);
	}
	
	public static int getLine2InLine1Count(String line2,String line1) {
		int count = 0;
		int index = 0;
		
		while ((index = line1.indexOf(line2)) != -1) {
			count++;
			line1 = line1.substring(index + line2.length());
			
		}
		return count;
	}
}
package LXT;

import java.util.Scanner;

/*
 *                  使用String和StringBuffer实现字符串倒置 
 */
public class lxt5 {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("请输入字符串:   ");
		String str = input.next();
		String[] strArr = str.split(" ");
		
		for (int i = 0; i < strArr.length; i++) {
			StringBuffer mo = new StringBuffer(strArr[i]);
			mo = mo.reverse();
			System.out.println(mo);
		}	
	}
}
package LXT;
/*
 *  int[] arr = {33,22,55,11,66}
	分别使用String和StringBuffer按照如下格式输出
	arr[33, 22, 55, 11, 66]
 */
public class lxt6 {
	public static void main(String[] args) {
		//StringBuffer方法
		StringBuffer str = new StringBuffer();
		
		str.append("int[] arr = {33,22,55,11,66}");
		
		
		str.delete(str.indexOf("i"), str.indexOf("a"));
		str.replace(str.indexOf(" "), str.indexOf("}"), "[33, 22, 55, 11, 66]");
		str.deleteCharAt(str.indexOf("}"));
		System.out.println(str);
		
		System.out.println(arrayToString(new int[] {33,22,55,11,66}));
	}
	//String 方法
	public static String arrayToString(int[] arr) {
		String result = "";
		result += "arr[";
		for (int i = 0; i < arr.length; i++) {
			if (i == arr.length - 1) {
				result += arr[i];
			} else {
				result += arr[i] + ", ";
			}
		}
		result += "]";
		return result;
	}
}
package LXT;

import java.util.Arrays;
import java.util.Scanner;

/*
 *  把字符串中的字符进行排序  dfebcga
 */
public class lxt7 {
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		String line = new Scanner(System.in).next();
		System.out.println(sortString(line));
	}
	
	public static String sortString(String str) {
		byte[] bys = str.getBytes();
		Arrays.sort(bys);	
		return new String(bys);
	}
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值