String类
1)String类:构造字符串对象
2)String对象代表一组不可变的字符序列,也就是说String对象一旦创建就不能被改变,因为底层是通过final关键字来修饰的。如果内容改变则会创建一个新的String对象,返回到原地址中。
String类常用的构造方法
String s1 = new String();
String s2 = new String(String original);
String s3 = new String(char[] a);
String s4 = new String(char[] a,int startIndex,int count)
首先必须要明确:栈,堆,常量池,方法区等概念
栈(本地方法栈):native method stack【操作系统的本地方法所需要的空间,存放局部变量、引用】
堆:存放所有new出来的对象。
常量池:存放字符串常量和基本类型常量(public static final)
方法区:是一个独立区域,既不属于堆,也不属于栈,在类加载的时候只运行一次。所有对象数据共享区域,存储静态变量和普通方法、静态方法、常量、字符串常量(严格说存放在常量池,堆和栈都有)等类信息,
==和equals的区别
“==”比较的是两个字符串内存地址的数值是否相等,属于数值比较;
equals:比较的是两个字符串的内容,属于内容比较
代碼解釋
package com.ghl.demo;
import org.junit.Test;
public class StringDemo {
@Test
public void test1() {
String s1 = "ghl";
String s2 = "java";
String s4 = "java";
String s3 = new String("java");
System.out.println(s2 == s3);//false
System.out.println(s2.equals(s3));//true
/*
* 通过上面的两个输出你因该知道的是==和equals的区别?
* “==”比较的是两个字符串内存地址的数值是否相等,属于数值比较;
* equals:比较的是两个字符串的内容,属于内容比较
*/
System.out.println(s2 == s4);
System.out.println(s2.equals(s3));
System.out.println("===========");
String s5 = "ghlava";
//当使用intern()方法时,首先查询字符串常量池是否存在当前字符串,
//若不存在则将当前字符串复制到字符串常量池中,并返回字符串常量池中的引用。
String s6 = (s1 + s2).intern();
System.out.println(s5 == s6);//false
System.out.println(s5.equals(s6));//fals
}
}
字符串常用的方法
public int length() 获取当前字符串的长度
public char charAt(int index) 返回指定索引处的字符,若不存在 则会出现越界异常 因为String底层是一个char[]
public boolean equals(Object anObject) //比较两个字符串的内容是否相等
public boolean equalsIgnoreCase(String anotherString)//将此 String 与另一个 String 比较,不考虑大小写
public int compareTo(String anotherString)//按字典顺序比较两个字符串
public int indexOf(String s)//返回指定子字符串在此字符串中首次出现处的索引。
public int indexOf(String s ,int startpoint)// 返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。
public int lastIndexOf(String s)// 返回指定子字符串在此字符串中最后一次出现处的索引。
public int lastIndexOf(String s ,int startpoint) // 返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。
public boolean startsWith(String prefix)//测试此字符串是否以指定的前缀开始。
public boolean endsWith(String suffix)//测试此字符串是否以指定的后缀开始。
public boolean regionMatches(int firstStart,String other,int otherStart ,int length)//测试两个字符串区域是否相等。
public String substring(int startpoint) / 返回一个新的字符串, 根据指定位置截取
public String substring(int start,int end)//从指定位置开始到指定位置结束截取字符串,包括开始不包括结束
pubic String replace(char oldChar,char newChar)//字符串的替换,旧的字符串被新的字符串所替换
public String replaceAll(String old,String new)
public String trim()//去除字符串前后空格 不去除中间空格
public String concat(String str)//字符串的连接
public String[] split(String regex)//按照指定某个字符切割字符串
==更多更详细的方法请参考jdk官方文档,讲的很详细==
字符串与基本数据类、包装类型之间的转换
务必记住:前者转后者就去后者找相应的方法。
1)字符串转换为基本数据类型、包装类,调用相应包装类的parseXXX(String str);
@Test
public void parseInt() {
String str = "10";
int parseInt = Integer.parseInt(str);
System.out.println(parseInt);
}
2)基本数据类型、包装类转换为字符串:调用字符串重载的valueOf() 方法
@Test
public void toStr() {
String str = "10";
int parseInt = Integer.parseInt(str);
String stringValueOf = String.valueOf(parseInt);
System.out.println(stringValueOf);
}
字符串与字节数组之间的转换
1)字符串转换为字节数组:调用字符串的getBytes()方法
String str="My Name Is ZhangSan";
byte[] b=str..getBytes();
......遍历输出值 等等
2)字节数组转换为字符串:调用字符串的构造方法,直接把字节数组传进去即可
String str="My Name Is ZhangSan";
byte[] b=str..getBytes();
......遍历输出值 等等
String str2=new String(b);
字符串与字符数组之间的转换(跟字节数组类似)
1)字符串转换成字符数组:调用字符串的toCharArray();
2)字符数组转换字符串: 调用字符串的构造方法,直接把字符数组传进去即可
StringBuffer类
1) 代表可变的字符序列,可以对字符串内容进行增删。
2)很多方法与String相同,但StingBuffer是可变长度的。而String是长度不可变的字符序列
3)StringBuffer是一个容器。因为底层是数组
4)我们如果对字符串进行拼接操作,每次拼接,都会构建一个新的String对象,既耗时,又浪费空间。而StringBuffer就可以解决这个问题
线程安全的可变字符序列
5)StringBuffer和String的区别
前者长度和内容可变,后者不可变。
如果使用前者做字符串的拼接,不会浪费太多的资源。
StringBuffer的构造方法
1)StringBuffer()初始容量为16的字符串缓冲区
2)StringBuffer(int size)构造指定容量的字符串缓冲区
3)StringBuffer(String str)将内容初始化为指定字符串内容
StringBuffer常用的方法
StringBuffer的追加方法,可以添加不同数据类型的数据
StringBuffer append(String s), StringBuffer append(int n) ,
StringBuffer append(Object o) , StringBuffer append(char n),
StringBuffer append(long n), StringBuffer append(boolean n),
StringBuffer insert(int index, String str) //在指定位置添加字符串
public StringBuffer reverse() //字符串的反转
StringBuffer delete(int startIndex, int endIndex) //从指定位置开始到指定位置结束删除指定索引的字符串
public char charAt(int n ) //取指定位置的字符
public void setCharAt(int n ,char ch)//在指定位置添加字符
StringBuffer replace( int startIndex ,int endIndex, String str)//在指定位置开始 到指定位置结束 替换某个字符
public int indexOf(String str)
public String substring(int start,int end)
public int length()
总结:
增
StringBuffer append(data);尾部添加
StringBuffer insert(index,data);指定未知插入
删
StringBuffer delete(int start, int end)
StringBuffer deleteCharAt(int index):删除指定位置的元素
改
StringBuffer replace(start,end,string);
void setCharAt(index,char);
查
char charAt(index);
int indexOf(string);
StringBuilder
功能和StringBuilder几乎一模一样,但是无线程安全,从1.5版本开始
为了提高效率,在单线程中尽量使用StringBuilder。
String,StringBuffer,StringBulider的区别
1)这三个类之间的区别主要是在两个方面,即运行速度和线程安全这两方面。
2)在执行速度上:StringBulider>StringBuffer>String
3) 在线程安全上:StringBuilder是线程不安全的,而StringBuffer是线程安全的
总结:
String:适用于少量的字符串操作的情况
StringBuilder:适用于单线程下在字符缓冲区进行大量操作的情况
StringBuffer:适用多线程下在字符缓冲区进行大量操作的情况