1.String类的常用方法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@Test public void test01() { String a = "abcdefghijk"; String b = "abcdefghijk"; System.out.println("字符串的长度:" + a.length()); System.out.println("比较两个字符串的内容是否相等:" + a.equals(b)); System.out.println("比较两个字符串的内容是否相等(忽略大小):" + a.equalsIgnoreCase(b)); System.out.println("把字符串的内容变成大写:" + a.toUpperCase()); System.out.println("把字符串的内容变成小写:" + a.toLowerCase()); }
2.把字符串转换成字符数组输出
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@Test public void test02() { String a = "abcdefj"; char[] b = a.toCharArray(); for (int i = 0; i < b.length; i++) { System.out.println(b[i]); } }
3.根据指定的分隔符把字符串拆分成字符串数组
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@Test public void test03() { String a = "a+b+c+d"; String[] b = a.split("\+"); for (int i = 0; i < b.length; i++) { System.out.println(b[i]); } }
4.查询@在字符串中第一次出现的位置
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@Test public void test04() { String a = "abind@oandf.doain@aidjf"; int index = a.indexOf("@"); System.out.println(index); }
5.查询@在字符串中最后一次出现的位置
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@Test public void test05() { String a = "abind@oandf.doain@aidjf"; int index = a.lastIndexOf("@"); System.out.println(index); }
6.查到@所在的下标后,并截取之后所有的字符串,包含@所在的下标
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@Test public void test06() { String a = "abind@oandf.doain@aidjf"; int index = a.indexOf("@"); String b = a.substring(index); System.out.println(b); }
7.从@符号的下标包含所在的下标,到最后一个@符号(不包含最后一个@符号的下标)之间的所有字符串
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@Test public void test07() { String a = "abind@oandf.doain@aidjf"; int index1 = a.indexOf("@"); int index2 = a.lastIndexOf("@"); String b = a.substring(index1, index2); System.out.println(b); }
8.去除字符串前后两边的空格
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@Test public void test08() { String a = " IndexOf lastIndexOf "; System.out.println("去除空格之前" + a); a = a.trim(); System.out.println("去除空格之后" + a); }
9.把字符i替换成I
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@Test public void test09(){ String a = "indexOf"; System.out.println("替换之前:"+a); a = a.replace("i", "I"); System.out.println("替换之后:"+a); }
10.查询字符串是否以i开始,是否以i结尾
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@Test public void test10() { String a = "indexOf"; System.out.println("是否以i开始:" + a.startsWith("i")); System.out.println("是否以i结尾:" + a.endsWith("i")); }
11.根据下标截取对应的字符
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@Test public void test11() { String a = "indexOf"; char b = a.charAt(3); System.out.println(b); }
12.将字符存储在字节数组中,最后再转换成字节输出
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@Test public void test12() { String a = "indexOf"; byte[] bytes = a.getBytes(); for (byte b : bytes) { System.out.println((char) b); } }
13.将字符串a和字符串b连接起来
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@Test public void test14() { String a = "index"; String b = "Of"; System.out.println(a + b); System.out.println(a.concat(b)); }
14.concat和+的区别
01.concat只能连接字符串
02.+可以连接任何类型。+默认是Java的String类的一种重载,将+后面的对象转换成String类型,然后再将字符串拼接,最后产生一个新的对象
15.查看字符串中是否包含i这个字符
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@Test public void test15() { String a = "indexOf"; System.out.println("是否包含i这个字符:" + a.contains("i")); }
16.String、StringBuffer和StriggBuilder三者的区别
01.在线程安全方面
001.String:数据量小的时候使用,线程是安全的
002.StringBuffer:线程安全
003.StringBuilder:多线程不安全,适合单线程
02.是否可变
001.String:是不可变的,每当我们操作字符串的时候,都会创建一个新的对象。
002.StringBuilder、StringBuffer:当我们操作字符串的时候,实际上是在操作同一个对象
03.在执行速度方面
StringBuilder大于StringBuffer大于String
17.String的执行速度
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@Test public void test16() { // 定义一个变量 String a = "indexOf"; // 定义操作字符串的次数 int count = 3000; // 设置开始时间 long beginTime = System.currentTimeMillis(); for (int i = 0; i < count; i++) { a += "indexOf"; } // 设置结束时间 long endTime = System.currentTimeMillis(); System.out.println("String的执行时间:" + (endTime - beginTime)); }
18.StringBuffer的执行速度
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@Test public void test17() { // 定义一个变量 String a = "indexOf"; StringBuffer buffer = new StringBuffer(a); // 定义操作字符串的次数 int count = 3000; // 设置开始时间 long beginTime = System.currentTimeMillis(); for (int i = 0; i < count; i++) { buffer = buffer.append("indexOf"); } // 设置结束时间 long endTime = System.currentTimeMillis(); System.out.println("String的执行时间:" + (endTime - beginTime)); }
19.StringBuilder的执行速度
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@Test public void test17() { // 定义一个变量 String a = "indexOf"; StringBuilder builder = new StringBuilder(a); // 定义操作字符串的次数 int count = 3000; // 设置开始时间 long beginTime = System.currentTimeMillis(); for (int i = 0; i < count; i++) { builder = builder.append("indexOf"); } // 设置结束时间 long endTime = System.currentTimeMillis(); System.out.println("String的执行时间:" + (endTime - beginTime)); }