1.String类
String是一个字符串对象
String是final的,不能被继承,被赋值不能改变
String对象字符内容都存放在char[] value数组中
String代表不可变的字符序列,称不可变性
实现了serializable接口,是可序列化的
实现了comparable接口,可以比较大小
1.1不可变性
由字面量对比可得知,两个String对象地址值是相同的
常量池中不会存储相同内容字符串
对字符串重新赋值,拼接,使用replace替换
都不可在原有的位置赋值,要重新指定内存区域
2.构造器|方法
2.1常用构造器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| byte[] arr1 = {97,98,99}; String s2 = new String(arr1); System.out.println(s2);
byte[] arr2 = {97,98,99,100,101}; String s3 = new String(arr2,2,3); System.out.println(s3);
char[] arr3 = {'M','I','P','H','O','N','E'}; String s4 = new String(arr3); System.out.println(s4);
String s5 = new String(arr3,3,4); System.out.println(s5);
String s6 = new String("Java"); System.out.println(s6);
|
2.2方法一
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| String character = "technology";
System.out.println(character.length());
System.out.println(character.charAt(3));
System.out.println(character.isEmpty());
System.out.println(character.toUpperCase()); System.out.println(character.toLowerCase());
String fruit = " a ppl e "; String afterfruit = fruit.trim(); System.out.println("-"+afterfruit+"-");
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| String character1 = "hello"; String character2 = "HellO";
System.out.println(character1.equals(character2)); System.out.println(character1.equalsIgnoreCase(character2));
System.out.println(character1.compareTo(character2)); System.out.println(character1.compareToIgnoreCase(character2));
String character3 = "GermanyScotland"; System.out.println(character3.substring(7)); System.out.println(character3.substring(0,6));
String world = character2.concat("world"); System.out.println(world);
|
2.3方法二
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| String s1 = "helloworld";
System.out.println(s1.endsWith("ld")); System.out.println(s1.startsWith("ld"));
System.out.println(s1.startsWith("ll",2));
String s2 = "world"; System.out.println(s1.contains(s2));
System.out.println(s2.indexOf("ld"));
System.out.println(s2.indexOf("ld",5));
System.out.println(s2.lastIndexOf("wo")); System.out.println(s2.lastIndexOf('d',4));
|
2.4方法三
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
String s1="NanKingg"; String s2 = s1.replace("King"," Company");
System.out.println(s2); String s3 = s1.replace('g','o'); System.out.println(s3);
String str = "nan23zhong32song32"; System.out.println(str.replaceAll("\\d+",","));
String str ="123242";
System.out.println(str.matches("\\d+"));
String str2 = "0571-4518716"; System.out.println(str2.matches("0571-\\d{7,8}"));
String str = "hello|world|us";
String[] split = str.split("\\|"); for (int i = 0; i < split.length; i++) { System.out.print(split[i]+" "); }
|
3.类型转换
3.1与char[]和包装类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| int a = 100; String s5 = String.valueOf(a); System.out.println(s5);
String str = "1548"; int num = Integer.parseInt(str); System.out.println(num);
char[] arr = {'J','E','F','E','N','G'}; System.out.println(new String(arr));
String s3="Hello"; char[] arr4 = s3.toCharArray(); System.out.println(Arrays.toString(arr4));
|
3.2与字节数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| String str = "abc淮信"; byte[] arr = str.getBytes();
System.out.println(Arrays.toString(arr));
String s2="机蜂程序员"; try { byte[] arr2=s2.getBytes("UTF-16"); System.out.println(Arrays.toString(arr2)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); }
byte[] s3 = {'J','E','F','E','N','G'}; System.out.println(new String(s3));
try { System.out.println(new String(s3,"UTF-16")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); }
|
4.Buffer|Builder
这两个类都是jdk1.5后新增的,常用方法相似
Buffer相对线程安全,开发根据需求使用
4.1常用方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| 添加区:
append(char c)
insert(offset,str)
删除区:
delete(int star,int end)
deleteCharAt(int index)
delete(0,参数名.length())
修改区: setCharAt(int index,char ch) replace(int start,int end,Stirng s) reverse()
截取区: substring(star) substring(star,end)
|
4.2类型转换方法
String转换SB
- 通过SB的构造方法实现
- 通过SB的append方法实现
SB转换String
- 通过String的构造方法
- 通过String的ToString()
- 通过substring()实现
5.面试题
5.1为什么new两个对象
1
| String a= new String("中国");
|
相当于new了两个一个在堆中,一个在常量池中
常量与常量拼接在常量池中,常量池不会存在相同常量
只要其中有一个变量,结果就在堆中
使用intern方法后,此对象一定实在常量池声明的
5.2三String的区别
StringBuffer 线程安全 可变字符序列 效率低 底层char[] Jdk1.5
StringBuilder 线程不安全 可变字符序列 效率高 底层char[] Jdk1.5
String 线程安全 不可变的字符序列 效率低 底层char[] Jdk1.0
通过底层源码理解
- String底层创建char[]为空或指定元素的索引长度
- StringBuffer和StringBuilder底层创建了索引为16的char[]
- 当数据长度超过底层索引16长度,默认扩容为原来容量二倍+2
- 同时把原有数组元素复制到新的数组中