Java学习笔记 --常用类之字符串相关类(二)常用方法

二、String类的常用方法
  1. int length():返回字符串的长度
	@Test
	public void MethodTest1() {
		String s1 = "hello";
		System.out.println(s1.length());// 5
	}
  1. char charAt(int index):返回字符串中指定索引处的字符
	@Test
	public void MethodTest2() {
		String s1 = "hello";
		System.out.println(s1.charAt(0));// h
		System.out.println(s1.charAt(4));// o
		System.out.println(s1.charAt(5));// StringIndexOutOfBoundsException
	}
  1. boolean isEmpty():判断字符串是否为空,判断标准为当前数组的length
    public boolean isEmpty() {
        return value.length == 0;
    }
	@Test
	public void MethodTest3() {
		String s1 = "hello";
		System.out.println(s1.isEmpty());// false
		String s2 = "";
		System.out.println(s2.isEmpty());// true
		String s3 = new String();
		System.out.println(s3.isEmpty());// true
	}
  1. String toLowerCase():将String中所有字符转换为小写
  2. String toUpperCase():将String中所有字符转换为大写
	@Test
	public void MethodTest4() {
		String s1 = "hello";
		String s2 = s1.toUpperCase();
		String s3 = s2.toLowerCase();
		System.out.println(s1);// hello --》 s1没有被更改
		System.out.println(s2);// HELLO
		System.out.println(s3);// hello
	}
  1. String trim():返回字符串的副本,忽略前导空白和尾部空白
	@Test
	public void MethodTest5() {
		String s1 = "  h e l l o  ";
		String s2 = s1.trim();
		System.out.println("---" + s1 + "---");//---  h e l l o  ---
		System.out.println("---" + s2 + "---");//---h e l l o---
	}
  1. boolean equals(Object obj):比较字符串内容是否相同
  2. boolead equalsIgnoreCases(String anotherString):忽略大小写,比较内容
	@Test
	public void MethodTest6() {
		String s1 = "hello";
		String s2 = "Hello";
		System.out.println(s1.equals(s2));// false
		System.out.println(s1.equalsIgnoreCase(s2));// true
	}
  1. String concat(String str):将指定字符串连连接导此字符串的结尾,等价于“+”
	@Test
	public void MethodTest7() {
		String s1 = "hello";
		String s2 = s1.concat(" world");
		System.out.println(s1);// hello
		System.out.println(s2);// hello world
	}
  1. int compareTo(String anotherString):比较字符串大小,返回第一个不相同字符之间的差值
	@Test
	public void MethodTest8() {
		String s1 = "abc";// a-97
		String s2 = " abc";// ' '= 32
		System.out.println(s1.compareTo(s2));// 65
	}
  1. String subString(int begiIndex):取现有字符串的子字符串,返回新字符串,该字符串从biginIndex开始截取到结尾
  2. String subString(int beginIndex, int endIndex):返回新字符串,该字符串从biginIndex开始截取,到endIndex结尾(不包含endIndex所在字符)
	@Test
	public void MethodTest9() {
		String s1 = "hello";
		String s2 = s1.substring(1);
		String s3 = s1.substring(1, 4);
		System.out.println(s1);// hello
		System.out.println(s2);// ello
		System.out.println(s3);// ell
	}
  1. boolean endsWith(String suffix):测试此字符串是否以指定后缀结束
  2. boolean startsWith(String prefix):测试此字符串是否以指定前缀开始
  3. boolean startsWith(String prefix, String toffest):测试此字符串是否以指定索引的子字符串开始
	@Test
	public void MethodTest10() {
		String s1 = "hello";
		System.out.println(s1.endsWith("lo"));// true
		System.out.println(s1.startsWith("Ho"));// false
		System.out.println(s1.startsWith("ll", 1));// false
		System.out.println(s1.startsWith("ll", 2));// true
	}
  1. boolean contains(CharSequence s):当且仅当字符串包含指定的char值序列时,返回true
	@Test
	public void MethodTest11() {
		String s1 = "hello";
		System.out.println(s1.contains("He"));// false
		System.out.println(s1.contains("el"));// true
	}
  1. int indexOf(String str):返回指定字符串在此字符串中第一次出现的索引
  2. int indexOf(String str, int fromIndex):返回指定子字符串在此字符串中从fromIndex开始从前往后第一次出现的索引
  3. int lastIndexOf(String str):返回指定字符串在此字符串中最后一次出现的索引
  4. int lastIndexOf(String str, int fromIndex):返回指定元素str从fromIndex开始从后往前开始第一次出现的索引
    indexOf和lastIndexOf方法如果未找到指定元素返回值都是-1
	@Test
	public void MethodTest12() {
		String s1 = "hello world!";
		System.out.println(s1.indexOf("o"));// 4
		System.out.println(s1.indexOf("l"));// 2
		System.out.println(s1.indexOf("l", 3));// 3
		System.out.println(s1.lastIndexOf("l"));// 9
		System.out.println(s1.indexOf("ll"));// 2
		System.out.println(s1.lastIndexOf("ll"));// 2
		System.out.println(s1.lastIndexOf("o", 3));// -1
	}
  1. String replace(char oldChar, char newChar):返回一个新的字符串,将原字符串中的oldChar替换为newChar
  2. String replace(CharSequence target, CharSequence replacement):
	@Test
	public void MethodTest13() {
		String s1 = "million";
		String s2 = s1.replace('m', 'b');
		String s3 = s1.replace("lion", "k");
		System.out.println(s1);// milllion
		System.out.println(s2);// billlion
		System.out.println(s3);// milk
	}
  1. String replaceAll(String regex, String replacement)
    regex: 正则表达式
	@Test
	public void MethodTest14() {
		String str = ("12hello34world5java7891mysql456");
		String string1 = str.replaceAll("\\d+", ",");// 将数字替换为“,”
		String string2 = string1.replaceAll("^,|,$", "");// 将首(^)尾(&)的“,”去掉
		System.out.println(string1);// ,hello,world,java,mysql,
		System.out.println(string2);// hello,world,java,mysql
	}
  1. String replaceFirst(String regex, String replacement)
	@Test
	public void MethodTest15() {
		String str = ("123def123");
		String string = str.replaceFirst("\\d+", "abc");
		System.out.println(string);// abcdef123
	}
  1. boolean matches(String regex):判断字符串中是否包含指定正则表达式中的字符
	@Test
	public void MethodTest16() {
		String str = "12345";
		// 判断str是否全部由数字组成,即有1-n个数字组成
		boolean matches = str.matches("\\d+");
		System.out.println(matches);// true

		String tel = "010-1234567";
		// 判断tel是否由“010-”开始,且后面接有7-8位数字
		boolean result = tel.matches("010-\\d{7,8}");
		System.out.println(result);// true
	}
  1. String[] split(String regex):根据给定正则表达式拆分字符串
	@Test
	public void MethodTest17() {
		String str = "hello|world|java";
		String[] strs = str.split("\\|");
		for (int i = 0; i < strs.length; i++) {
			System.out.println(strs[i]);
		}
	}
hello
world
java
  1. String[] split(String regex, int limit):根据给定正则表达式拆分字符串,结果数组长度最大为limit
	@Test
	public void MethodTest18() {
		String str = "hello|world|java|hello|world|java";
		String[] strs = str.split("\\|", 4);
		for (int i = 0; i < strs.length; i++) {
			System.out.println(strs[i]);
		}
	}
hello
world
java
hello|world|java
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值