String类
1、定义:
String str = hello; // 直接赋值
String str2 = new String(“hello”); //new 一个对象
char [] ch = {‘a’,‘b’.‘c’};
String str3 = new String(ch);
2、在内存中的存储形式
(1)双引号 引起的内容 均存放在常量池;
(2)若常量池当中的存在该字符串常量 则不会将该字符串再放到常量池中
(3)每new一次 就产生一个新的对象(存放在堆上)
(4)String的引用str放在jvm栈上,里面存放着对象的地址
字符串的比较
1、str == str2 比较的是字符串的地址
2、str.equals(str2) 比较的是字符串的内容
public static void main(String[] args) {
String str1 = "abcdef";
String str2 = new String("abcdef");
System.out.println(str1 == str2);//false
String str3 = new String("abcdef");
System.out.println(str2 == str3);//false
//如果两个字符串常量直接拼接+ 那么在编译期间就处理为
//拼接的结果
String str4 = "ab"+"cdef";//abcdef final int a = 10
System.out.println(str1 == str4);//true
String str5 = "ab" + new String("cdef");
System.out.println(str1 == str5);//false
System.out.println(str1.equals(str5));//true
System.out.println(str5);
String str6 = "ab";
String str7 = "cdef";
String str8 = str6+str7;
System.out.println(str1 == str8);// false
System.out.println(str1.equals(str8));
String str9 = str6+"cdef";
System.out.println(str1 == str9);
String str10 = "hello" + 10;//"hello10"
String str11 = "hello10";
System.out.println(str10 == str11);
}
个别在内存中的图;
3、使用equals注意实项:
对于 str.equals(“abcd”);
若str 为null ,此时代码就会异常,
所有当使用equals时,应把上述代码改为 “abc”.equals(str);
字符串常量池
1、对于String类的实例化有两种,一种是直接赋值,一种是new一个新的String、
(1)直接赋值:
String str = "aaa"; String str2 = "aaa"; System.out.println(str == str2); //true
如果现在采用了直接赋值的模式进行String类的对象实例化操作,那么该实例化对象(字符串内容)将自动保存到这个对象池之中.“即引号引起的都会存放在常量池中”
如果下次继续使用直接赋值的模式声明String类对象,此时对象池之中如若有指定内容,将直接进行引用
如若没有,则开辟新的字符串对象而后将其保存在对象池之中以供下次使用
(2)采用构造方法
即new
String str = new Sting("aaa");
此时会在堆上开放两个内存,一个存放new的对象(其中保存的是常量池aaa的地址),另一块即在常量池存放“aaa”
(3)intern();方法
// 该字符串常量并没有保存在对象池之中
String str2 = new String("hello") ; String str1 = "hello" ; System.out.println(str1 == str2);
// 执行结果 false
String str2 = new String("hello").intern() ; String str1 = "hello" ; System.out.println(str1 == str2);
// 执行结果 true
inter(); 如果常量池当中有引用,那么直接将此引用返回回去,进行赋值
字符串不可变
String str = "hello" ; str = str + " world" ; str += "!!!" ; System.out.println(str);
// 执行结果
hello world!!!
对于上述代码表面上修改了字符串, 实际上每次变化都等同于new了一个新对象(每次str的地址都不一样);
若真的要修改字符串的内容;可以采用反射的方法
字符与字符串
1、字符串和字符数组
public static void main(String[] args) {
char[] value = {'a','b','c','d','e'};
String str = new String(value);
System.out.println(str);
//offset:偏移量
//从偏移量offset位置开始的count个字符来构造字符串
//运行时期的异常:StringIndexOutOf.....
String str2 = new String(value,1,4);
System.out.println(str2);
String str3 = "abcdef";
//.....charAt(参数:下标-》0)
/*char ch = str3.charAt(4);
System.out.println(ch);*/
System.out.println(str3.charAt(3));
//字符串-》数组
char[] crray = str3.toCharArray();
System.out.println(Arrays.toString(crray));
}
2、字符串和字节数组
String str = "helloworld" ;
// String 转 byte[]
byte[] data = str.getBytes() ;
for (int i = 0; i < data.length; i++) {
System.out.print(data[i]+" ");
}
// byte[] 转 String
System.out.println(new String(data));
字符串常见操作
1、比较内容
(1)equals(); //区分大小写
(2)equalsIgnoreCse(); //不区分大小写
`String s = “abc”;
String s1 = “Abc";
System.out.println(s.equals(s1)); //flase
System.out.println(s.equalsIgnoreCse(s1)); //true
(3)comparTo():
相等 =0
小于 返回内容小于0
大于 返回大于0
System.out.println(str1.compareTo(str2));
2、字符串查找
1、contains(); 是否包含
String str = "helloworld" ; System.out.println(str.contains("world")); // true
2、使用indexOf()方法进行位置查找
String str = "helloworld"; System.out.println(str.indexOf("l")); // 2 System.out.println(str.indexOf("l",5)); // 8 System.out.println(str.lastIndexOf("l")); // 8
3、字符串替换
1、replaceAll();
全部替换
2、replaceFirst();替换第一个
String str = “helloworld” ;
System.out.println(str.replaceAll(“l”, “L”)); //heLLoworLd
System.out.println(str.replaceFirst(“l”, “L”)); //heLoloworld
4、字符串拆分
1、split(); 全部拆分
String str = "hello world hello " ;
String[] result = str.split(" ") ; // 按照空格拆分
for(String s: result) {
System.out.println(s);
}
// 结果
{hello,word,hello}
2、split("",int limit); 部分拆分 按长度极限
String str = "hello world hello" ;
String[] result = str.split(" ",2) ;
for(String s: result) {
System.out.println(s);
}
//结果
{hello,word hello}
字符串截取
1、substring(int index);指定位置开始截取
2、substring(int start,int end);部分截取
其他
StringBuffer 和StringBulider
面试题:请解释String、StringBuffer、StringBuilder的区别:
1、String的内容不可修改,StringBuffer与StringBuilder的内容可以修改.
2、StringBuffer与StringBuilder大部分功能是相似的
3、StringBuffer采用同步处理,属于线程安全操作;而StringBuilder采用异步处理,属于线程不安全操作
1、String 转换成 StringBuffer/StringBulider
利用toString();方法
StringBuffer stringBuffer = new StringBuffer("abcdef");
String s = stringBuffer.toString();
System.out.println(s);
StringBulider 同理
2、StringBuilder/StringBuffer 转换成string
利用stringbuffer的构造方法或append();方法
String str = "abcdef";
/*StringBuffer stringBuffer = new StringBuffer(str);
System.out.println(stringBuffer);*/
StringBuffer stringBuffer = new StringBuffer();
tringBuffer.append(str);
System.out.println(stringBuffer);
StringBuilder同理