Java高级之String的常用方法

Module1:
package com.string.exer;

public class StringMethodTest {
	public static void main(String[] args) {
		
		String str = "Hello Big Data";
		
		// 1. 获取字符串的长度
		int len = str.length();
		System.out.println(len);
		
		// 2. 返回某处索引的字符
		char c = str.charAt(0);
		System.out.println(c);
		
		// 3. 判断是否是空字符串
		System.out.println(str.isEmpty());
		
		// 4. 将字符串转为大写
		String str2 = str.toUpperCase();
		System.out.println(str2); // 修改后的
		System.out.println(str); // str不变,因为字符串不可变
		
		// 5. 将字符串转为小写
		String str3 = str.toLowerCase();
		System.out.println(str3);
		
		// 6. 去除字符串首尾的空格
		String str4 = "  hello  az  ";
		System.out.println("------" + str4.trim() + "------"); // 只去除首尾,中间的空格去不掉
		
	}
}

Module2:
package com.string.exer;

public class StringMethodTest2 {
	public static void main(String[] args) {
		
		// 1. 判断字符串是否相等
		String s1 = "hello";
		String s2 = "hello";
		System.out.println(s1.equals(s2));
		
		// 2. 判断字符串是否相等,忽略大小写
		String s3 = "Hello";
		String s4 = "hello";
		System.out.println(s3.equals(s4));
		System.out.println(s3.equalsIgnoreCase(s4)); // 忽略大小写
		
		// 3. 将指定字符串连接到此字符串的结尾,等价于"+"
		String s5 = "aaa";
		String s6 = s5.concat("bbb"); 
		System.out.println(s6);
		
		// 4. 比较两个字符串的大小
		String s7 = "aaa";
		String s8 = "bbb";
		System.out.println(s7.compareTo(s8)); // 返回值大于0说明s7大于s8,小于0说明s7小于s8,等于0二者相等
		
		// 5. 截取字符串,左闭右开
		String s9 = "你好啊南工";
		System.out.println(s9.substring(2));
		System.out.println(s9.substring(3, 5));
	}
}
Module3:
package com.string.exer;

import org.junit.jupiter.api.Test;

public class StringMethodTest3 {

	@Test
	public void test3() {
		// 1. 判断此字符串是否以指定的后缀结束
		String s1 = "helloworld";
		boolean b1 = s1.endsWith("rld");
		System.out.println(b1);

		// 2. 判断此字符串是否以指定的前缀开始
		boolean b2 = s1.startsWith("hell");
		System.out.println(b2);

		// 3. 测试此字符串以指定索引开始的字符串是否以指定的前缀开始
		boolean b3 = s1.startsWith("ll", 2);
		System.out.println(b3);

		// 4. 当且仅当此字符串包含指定的char值序列时,返回true
		boolean b4 = s1.contains("orl");
		System.out.println(b4);

		// 5. 返回指定字符串在当前字符串中第一次出现处的索引
		int t1 = s1.indexOf("ll");
		System.out.println(t1); // 没找到返回-1
		// 从指定开始索引位置后的字符串中查找指定字符串
		int t2 = s1.indexOf("ll", 4);
		System.out.println(t2);

		// 6. 从后往前找,返回值和indexOf一样
		String s2 = "hellorworld";

		int t3 = s2.lastIndexOf("or");
		System.out.println(t3);
		// 从当前指点的索引位置反向向前搜索
		int t4 = s2.lastIndexOf("or", 6);
		System.out.println(t4);
	}

	@Test
	public void test2() {
		// 1. 在此字符串中将指定字符串替换成新字符串,即是支持字符的替换
		String s1 = "你好南工,南工你好";
		String s2 = s1.replace("南", "东");
		System.out.println(s2);

		// 2. 使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串,即是支持字符串的替换
		String s3 = "abcdbf";
		String s4 = s3.replace("bc", "nn");
		System.out.println(s4);

		// 3. 使用给定的字符序列替换此字符串中所有匹配给定的正则表达式的子字符串
		String str5 = "12hello34world5java7891mysql456";
		String string = str5.replaceAll("\\d+", ",").replaceAll("^,|,$", "");
		System.out.println(string);

		// 4. 告知此字符串是否符合给定的正则表达式
		String str = "12345";
		
		// 判断str字符串中是否全部有数字组成,即有1-n个数字组成
		boolean matches = str.matches("\\d+");
		System.out.println(matches);
		
		String tel = "0571-4534289";
		//判断这是否是一个杭州的固定电话
		boolean result = tel.matches("0571-\\d{7,8}");
		System.out.println(result);
		
		// 5. 切分字符串
		String s5 = "hello|world|java";
		String[] strs = s5.split("\\|");
		for(String s : strs) {
			System.out.print(s + " ");
		}
	}

	public static void main(String[] args) {

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值