百知教育 - 孙帅 - 13_字符串
01_String的封装
-
字符串是字符数组封装而成的
-
代码:
package day14; public class TestString{ public static void main(String[] args){ char[] cs = {'h','e','l','l','o','w','o','r','l','d'}; System.out.println(cs); MyString ms = new MyString(cs); ms.toUpperCase(); System.out.println(ms); String s = "helloworld"; String s2 = s.toUpperCase(); System.out.println(s2); } } class MyString{ private char[] cs; public MyString(char[] cs){ this.cs = cs; } public void toUpperCase(){ for(int i = 0; i < cs.length; i++){ if(cs[i] >= 'a' && cs[i] <='z') cs[i] -= 32; } } public String toString(){ String s = ""; for(int i = 0; i < cs.length; i++){ s += cs[i]; } return s; } }
-
运行结果:
02_String的方法
-
与字符数组相关的方法
- toCharArray() 转成字符数组
- new String(char[] cs) 利用字符数组构造字符串
- toUpperCase()/toLowerCase() 大小写转换
-
与子串相关
- contains(String s) 判断s子串是否存在
- startsWith(String s) 判断字符串是否以s子串开头
- endsWith(String s) 判断字符串是否以s子串结尾
- indexOf(String s) 返回s子串最早出现的下标
- lastIndexOf(String s) 返回s子串最后出现的下标
- replace(String s1, String s2) 将s1子串替换为s2子串
- substring(int a, int b) 返回字符串中a下标到b下标的子串(包含a下标,不包含b下标)
- length() 返回字符串的长度
- split(String s) 将字符串以s为分隔符,拆分为多个子串
- trim() 去除字符串前后的回车与空格
-
代码:
package day14; public class TestString1 { public static void main(String[] args){ //String s = "abcdefghijklmnopqrstuvwxyz"; /* char[] cs = s.toCharArray(); for(int i = 0; i < cs.length; i++){ if(cs[i]>='a' && cs[i]<='z') cs[i] -= 32; } String s2 = new String(cs); System.out.println(s2); */ /* String s1 = "ABC"; String s2 = "ABD"; System.out.println(s1.compareTo(s2)); */ String s = "ab:nd:er:thsu:we"; String[] ss = s.split(":"); for(int i = 0; i < ss.length; i++){ System.out.println(ss[i]); } s = " abc "; System.out.println(s.trim()); } }
-
运行结果:
03_String不可变
-
字符串的内容不可变的作用
我们可以利用方法区里的串池去共享字符串对象
-
代码:
package day14; public class TestStringAppend{ public static void main(String[] args){ String s1 = "abc"; String s2 = "abc"; System.out.println(s1 == s2); s1 += "d"; System.out.println(s1); System.out.println(s2); System.out.println(s1 == s2); String s3 = new String("abc"); String s4 = new String("abc"); System.out.println(s3 == s4); } }
-
运行结果:
04_String累加
-
弊端:
String字符串连接时会产生大量的中间对象 -
解决方案:
使用StringBuilder创建对象,之后再调用toString()方法返回字符串。 -
System.nanoTime()方法
返回系统时间(long) -
代码:
package day14; public class TestStringAppend{ public static void main(String[] args){ /* String s1 = "abc"; String s2 = "abc"; System.out.println(s1 == s2); s1 += "d"; System.out.println(s1); System.out.println(s2); System.out.println(s1 == s2); String s3 = new String("abc"); String s4 = new String("abc"); System.out.println(s3 == s4); */ System.out.println(stringBuilderAppend()); System.out.println(stringAppend()); } static long stringAppend(){ long t1 = System.nanoTime(); String s = ""; for(int i = 1; i <= 100000; i++){ s = s +"A"; } long t2 = System.nanoTime(); return t2 - t1; } static long stringBuilderAppend(){ long t1 = System.nanoTime(); String s = ""; StringBuilder sb = new StringBuilder(s); for(int i = 1; i <= 100000; i++){ sb.append("A"); } s = sb.toString(); long t2 = System.nanoTime(); return t2 - t1; } }
-
运行结果:
-
代码(面试题):
package day14; public class TestExam{ public static void main(String[] args){ long t1 = System.nanoTime(); String s = "a"+"b"+"c"+"d"+"e"+"f"; long t2 = System.nanoTime(); System.out.println(t2-t1); long t3 = System.nanoTime(); String s2 = "a"; StringBuilder sb = new StringBuilder(s2); sb.append("b").append("c").append("d").append("e").append("f"); s2 = sb.toString(); long t4 = System.nanoTime(); System.out.println(t4-t3); } } // 与上题相比,此题String字符串直接累加速度反而快。因为在编译器有初级的运算功能,能直接计算出字符串字面值累加的结果. "a"+s 则算不出来.
-
运行结果: