对于String的说明
1.String类:声明为final的,不可被继承的
2.String实现了Serializable接口,表示字符串是支持序列化的
实现了Comparable接口,表示可以比较大小
3.String内部定义了final char[] value用来存储字符信息
4.String:代表不可变的字符序列。简称:不可变性
重新赋值时,只是改变了地址值,有一个新的数值
⭐5.通过字面量的方式直接赋值,数据放在常量区内。
⭐6.常量区相同的数据只会有一个
7.重新赋值,改变地址值
/* new的abc在堆里,abc2在常量池里 */
String abc = new String("abc");
String abc2 = "abc";
System.out.println(abc == abc2);
一道练习题
String s1 = "javaEE";
String s2 = "hedoop";
String s3 = "javaEEhadoop";
String s4 = "javaEE" + "hadoop";
String s5 = s1 + "hadoop";
String s6 = "javaEE" + s2;
String s7 = s1 + s2;
System.out.println(s3 == s4);//true
System.out.println(s3 == s5);
System.out.println(s3 == s6);
System.out.println(s5 == s6);
System.out.println(s3 == s7);
System.out.println(s5 == s6);
System.out.println(s5 == s7);
System.out.println(s6 == s7);//上面都是false
//只要是以 s1 + "jflaj"的方式 此时的变量在堆中
//intern() :返回值,返回常量池中的地址
String s8 = s5.intern();
System.out.println(s3==s8);//true
String常用的方法
//length
String s1 = "helloworld";
System.out.println(s1.length());
//char chatAt(int index)
System.out.println(s1.charAt(1));//e
//boolean isEmpty()
System.out.println(s1.isEmpty());
//toUpperCase()
System.out.println(s1.toUpperCase());
//toLowerCase()
System.out.println(s1.toLowerCase());
//trim().去掉前后空格
String s2 = " he llo world ";
System.out.println(s2.trim());
//boolean equals()
String s3 = "HelloWorld";
String s4 = "helloworld";
System.out.println(s1.equals(s2));
//equalsIgnoreCase():比较忽略大小写
System.out.println(s3.equalsIgnoreCase(s4));
//compareTo():比较大小
System.out.println(s3.compareTo(s4));
//subString(a,b):[a,b)
System.out.println(s3.substring(0, 5));
//subString(start)
System.out.println(s3.substring(5));
//endsWith(String suffix):测试是否以指定后缀结束
String s1 = "helloworld";
System.out.println(s1.endsWith("ld"));
//startWith(String prefix):是否以前缀开始
System.out.println(s1.startsWith("hel"));
//startWith(String prefix,int toffset):从toffset开始是否以prefix开始
System.out.println(s1.startsWith("ll", 2));
//contains(CharSequence s):是否包含s
System.out.println(s1.contains("owo"));
//indexOf(String s)
System.out.println(s1.indexOf("llo"));
//indexOf(String s,fromIndex):
System.out.println(s1.indexOf("llo", 5));
System.out.println(s1.indexOf("llo", 2));
//lastIndexOf(String s):返回也是从前往前的位置
System.out.println(s1.lastIndexOf("llo"));
//lastIndexOf(String s,int fromIndex):
System.out.println(s1.lastIndexOf("llo", 3));
System.out.println(s1.lastIndexOf("llo", 10));//???????
String s1 = "abcdefghijk";
//replace()
System.out.println(s1.replace('a', 'b'));
System.out.println(s1.replace("abc", "nnn"));
String s2 = "12341afjal15423ajfa";
System.out.println(s2.replaceAll("\\d+", "--"));
System.out.println(s2.replaceFirst("\\d+", "--"));
//matchs()
System.out.println(s2.matches("\\d+"));
//split()
String[] split = s2.split("\\d+");
for(int i = 0 ; i < split.length;i++){
System.out.print(split[i]);
}
System.out.println();
String[] split1 = s2.split("\\d+", 2);
for(int i = 0 ; i < split1.length;i++){
System.out.print(split1[i]);///?????
//运行结果
bbcdefghijk
nnndefghijk
--afjal--ajfa
--afjal15423ajfa
false
afjalajfa
afjal15423ajfa
String和基本数据类型的转换
//String 与基本数据类型的转换
String str1 = "123";
int i = Integer.parseInt(str1);
System.out.println(i);
//基本数据类型转换为String
String str2 = String.valueOf(i);
System.out.println(str2);
String与char[]的转换
// String -- > char[]
String str1 = "abc123";
char[] charArray = str1.toCharArray();
for(int i = 0 ; i < charArray.length;i++){
System.out.println(charArray[i]);
}
//char[] -- > String
String s = new String(charArray);
System.out.println(s);
String与byte[]数组的转换
//String --> byte[]
String s = "abc123中国中国";
byte[] bytes = s.getBytes();
System.out.println(Arrays.toString(bytes));
//byte[] --> String
String s2 = new String(bytes);
System.out.println(s2);
//gbk形式getBytes
byte[] gbks = s.getBytes("gbk");
System.out.println(Arrays.toString(gbks));
String s3 = new String(gbks,"gbk");
System.out.println(s3);
StringBuffer和StringBuilder的使用
String、StringBuffer、StringBuilder三者的异同?
String:不可变的字符序列;底层使用char[]存储
StringBuffer:可变的字符序列;线程安全的,效率低;底层使用char[]存储
StringBuilder:可变的字符序列;jdk5.0新增的,线程不安全的,效率高;底层使用char[]存储
StringBuffer的底层分析
源码分析:
String str = new String();//char[] value = new char[0];
String str1 = new String("abc");//char[] value = new char[]{'a','b','c'};
StringBuffer sb1 = new StringBuffer();//char[] value = new char[16];底层创建了一个长度是16的数组。
System.out.println(sb1.length());//
sb1.append('a');//value[0] = 'a';
sb1.append('b');//value[1] = 'b';
StringBuffer sb2 = new StringBuffer("abc");//char[] value = new char["abc".length() + 16];
//问题1. System.out.println(sb2.length());//3
//问题2. 扩容问题:如果要添加的数据底层数组盛不下了,那就需要扩容底层的数组。
默认情况下,扩容为原来容量的2倍 + 2,同时将原有数组中的元素复制到新的数组中。
指导意义:开发中建议大家使用:StringBuffer(int capacity) 或 StringBuilder(int capacity)
StringBuffer的使用
setCharAt()
StringBuffer sb1 = new StringBuffer("abc");
sb1.setCharAt(0,'m');
System.out.println(sb1);
StringBuffer sb2 = new StringBuffer();
System.out.println(sb2.length());//0
对比String、StringBuffer、StringBuilder三者的效率
从高到低排列:StringBuilder > StringBuffer > String
//初始设置
long startTime = 0L;
long endTime = 0L;
String text = "";
StringBuffer buffer = new StringBuffer("");
StringBuilder builder = new StringBuilder("");
//开始对比
startTime = System.currentTimeMillis();
for (int i = 0; i < 20000; i++) {
buffer.append(String.valueOf(i));
}
endTime = System.currentTimeMillis();
System.out.println("StringBuffer的执行时间:" + (endTime - startTime));
startTime = System.currentTimeMillis();
for (int i = 0; i < 20000; i++) {
builder.append(String.valueOf(i));
}
endTime = System.currentTimeMillis();
System.out.println("StringBuilder的执行时间:" + (endTime - startTime));
startTime = System.currentTimeMillis();
for (int i = 0; i < 20000; i++) {
text = text + i;
}
endTime = System.currentTimeMillis();
System.out.println("String的执行时间:" + (endTime - startTime));
StringBuffer常用方法
StringBuffer的常用方法:
StringBuffer append(xxx):提供了很多的append()方法,用于进行字符串拼接
StringBuffer delete(int start,int end):删除指定位置的内容
StringBuffer replace(int start, int end, String str):把[start,end)位置替换为str
StringBuffer insert(int offset, xxx):在指定位置插入xxx
StringBuffer reverse() :把当前字符序列逆转
public int indexOf(String str)
public String substring(int start,int end):返回一个从start开始到end索引结束的左闭右开区间的子字符串
public int length()
public char charAt(int n )
public void setCharAt(int n ,char ch)
总结:
增:append(xxx)
删:delete(int start,int end)
改:setCharAt(int n ,char ch) / replace(int start, int end, String str)
查:charAt(int n )
插:insert(int offset, xxx)
长度:length();
遍历:for() + charAt() / toString()
StringBuffer s1 = new StringBuffer("abc");
s1.append(1);
s1.append('1');
System.out.println(s1);
// s1.delete(2,4);
// s1.replace(2,4,"hello");
// s1.insert(2,false);
// s1.reverse();
String s2 = s1.substring(1, 3);
System.out.println(s1);
System.out.println(s1.length());
System.out.println(s2);