第三周收获

String常用方法使用

//将ABCDEFGHIJKLMN字符串以16进制的方式显示到控制台中?
String str = "ABCDEFGHIJKLMN";
for(int i = 0;i < str.length();i++) {
    char ch = str.charAt(i);

    String hex = Integer.toHexString(ch);
    System.out.println("0X"+hex);
}

//-15  比较两个字符串在字典的顺序
System.out.println("hello".compareTo("hOrld"));

System.out.println("hello".compareToIgnoreCase("hOrld"));

//字符串拼接
System.out.println("hello".concat("world"));

//判断目标字符串中是否包含指定的子串
System.out.println("helloworld".contains("wor"));

String path = "http://www.softeem.com/index/imgs/logo.png";
//判断目标字符串是否以指定的后缀结尾
System.out.println(path.endsWith(".png"));

//判断目标字符串是否以指定的子字符串开头
System.out.println(path.startsWith("http")); 

//判断指定指定的字符串在目标字符串中第一次出现的位置(索引)
System.out.println(path.indexOf("www"));

//判断目标String对象的length()返回值是否为0
System.out.println("".isEmpty());

//jdk8新增用于使用指定的分割符将不同的字符串使用该分隔符连接到一起
System.out.println(String.join("-", "hello","world","softeem"));

String常用方法练习

public class TestString {

	public void replace() {
		String commons = "听说单身狗凤姐去了米国,找到了心仪男票,三天两头在微博上抨击单身狗。。。。";
		String[] words = { "凤姐", "米国", "单身狗" };
		for (String k : words) {
			commons = commons.replace(k, placeholder(k));
		}
		System.out.println("替换后:" + commons);
	}

	// 根据字符串的字符个数返回对应长度的“*”
	public String placeholder(String s) {
		String p = "";
		for (int i = 0; i < s.length(); i++) {
			p += "*";
		}
		return p;
	}

	/**
	 * 手机号脱敏 将手机号的前三位后四位显示中间四位使用"*"替换
	 * 
	 * @param phoneNum
	 * @return
	 */
	public String handler(String phoneNum) {
		return phoneNum.substring(0,3)+"****"+phoneNum.substring(8); 
	}

	public static void main(String[] args) {

		String url = "http://www.softeem.com/query/list.html";
		// 获取指定的子字符串在目标字符串中最后一次出现的索引
		int i = url.lastIndexOf("/");
		System.out.println(i);
		System.out.println(url.substring(i + 1));

		// 匹配给定的字符串是否符合指定的正则表达式规则(匹配,查找,替换)
		System.out.println("13567898765".matches("1[3578]\\d{9}"));

		// 将字符串中指定的字符替换成新的字符
		System.out.println("无极剑圣剑客".replace('剑', '刀'));

		String commons = "听说单身狗凤姐去了米国,找到了心仪男票,三天两头在微博上抨击单身狗。。。。";

		System.out.println(commons.replace("单身狗", "***"));

		new TestString().replace();

		// 脱敏操作 134****7890
		System.out.println("张三13456543212男".replaceAll("1[3578]\\d{9}", "*"));
		
		String phone = new TestString().handler("13245674321");
		System.out.println(phone);
	
		String str = "10/张三/男/计算机科学与技术/68.8";
		//使用特定的分隔符对字符串截取,并存储到数组中
		String[] info = str.split("/");
//		for (String s : info) {
//			System.out.print(s + " ");
//		}
		Student stu = new Student();
		stu.setSno(Integer.parseInt(info[0]));
		stu.setSname(info[1]);
		stu.setSex(info[2]);
		stu.setMajor(info[3]);
		stu.setScore(Double.parseDouble(info[4]));
		
		System.out.println(stu);
		
		String file = "美女.png";
		System.out.println(file.split("\\.")[0]);
		
		//将指定的字符串转换为大写字母
		System.out.println("helloworld".toUpperCase());
		//将字符串转换为小写
		System.out.println("HelloWorld".toLowerCase());
		
		//将字符串前后空格去除
		System.out.println("    hello world   123   ".trim());
		
		//有以下学生名称   smith scott allen james kobe
		//要求将学生名字首字符大写输出?
		
		String name = "   smith scott allen james kobe   ";
		String[] names = name.trim().split(" ");
		for (String n : names) {
			n = n.substring(0,1).toUpperCase()+n.substring(1).toLowerCase();
			System.out.println(n);
		}
		
		//将整数类型值转换为String
		System.out.println(String.valueOf(100));
	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值