String类
一、String的简单学习
String类在Java中使用final关键字修饰,属于final类,不可被继承,并且String类中的所有成员方法都是final方法。通过以下代码对String类做一个简单的了解。
String stra = "hello"; String strb = "hello"; String strc = "he"+"llo"; String str1 = "he"; String str2 = "llo"; String strd = str1 + str2; final String str3 = "he"; final String str4 = "llo"; String stre = str3 + str4; String strf = "he" + new String("llo"); String strg = new String("hello"); String strh = new String("hello").intern(); System.out.println(stra == strb); //true System.out.println(stra == strc); //true System.out.println(stra == strd); //false System.out.println(stra == stre); //true System.out.println(stra == strf); //false System.out.println(stra == strg); //false System.out.println(stra == strh); //true
“stra == strb”的结果为true,stra与strb都是指向“hello”字符串对象,字符串常量对象都保存在字符串常量池中,这两个“hello”实际上是同一个对象;
“stra == strb”的结果为true,strc指向字符串常量“he”和“llo”拼接后的字符串对象,也就是“hello”,字符串常量池中只存在一个“hello”;
“stra == strd”的结果为false,strd指向的对象由变量str1和str2拼接而来,JAVA中字符串变量的拼接操作都是通过StringBulder对象的append()来完成,也就是说通过字符串变量str1与str2的拼接给strd传值,相当于先创建一个StringBuilder对象,然后使用StringBuilder的append()方法将str1和str2所指向的字符串对象添加到StringBuilder对象中,再调用toString()转换成一个保存在堆内存中的“hello”字符串对象,将对内存中的“hello”对象的引用传递给strd,而stra指向的是字符串常量池中的“hello”,所以两个对象的所指向的地址并不相同;
“stra == stre”的结果为true,stre所指向的对象由两个用final修饰的String变量拼接成,在Java中由final关键字定义的变量相当于常量,所以stre指向的对象相当于是字符串常量“he”与“llo”的拼接,指向的还是字符串常量池中的“hello”对象;
“stra == strf”的结果为false,strf为字符串常量“he”与字符串匿名对象“new String("llo")”的拼接,会生成一个新的字符串对象“hello”保存在堆内存之中,然后将这个对象的引用传递给strf对象;
“stra == strg”的结果为false,strg指向的是使用new关键字在堆内存中开辟的“hello”对象;
“stra == strh”的结果为true,strh对象使用new String("hello").intern()声明,与直接用new String("hello")声明不同的是,strh对象的声明使用String类中的intern()将“hello”字符串添加到字符串常量池中,String类中对intern()方法的定义是,先在字符串常量池中查找有没有目标字符串,若有则返回当前字符串,没有则将目标字符串添加到字符串常量池中再返回,所以strh指向的是字符串变量池中的“hello”。
二、String类中的常用方法
| 字符与字符串 | |
| public String(char[] value) | 将字符数组转换为String类对象 |
| public String(char[] value,int offset,int count) | 将部分字符数组转换为String类对象 |
| public char charAt(int index) | 返回知道索引对应的字符信息 |
| public char[] toCharArray() | 将字符串以字符数组的形势返回 |
| 字节与字符串 | |
| public String(byte[] bytes) | 将字节数组转换为String类对象 |
| public String(byte[] bytes,int offset,int length) | 将部分字节数组转换为String类对象 |
| public byte[] getBytes() | 将字符串变为字节数组 |
|
public byte[] getBytes(String charsetName) |
进行编码转换 |
| 字符串比较 | |
| public boolean equals(String anString) | 进行字符串比较,区分大小写 |
| public boolean equalsIgnoreCase(String anotherString) | 忽略大小写进行字符串比较 |
| public int compareTo(String anotherString) |
判断两个字符串的大小(按字符编码比较)返回结果: =0 : 字符串内容相等 >0 : 前面的字符串较大 <0 : 前面的字符串较小 |
| 字符串查找 | |
| public boolean contains(CharSequence s) | 判断知道内容是否存在 |
| public int indexOf(String str) |
由前向后查找指定字符串的位置,若存在则返回位置索引, 不存在则返回 -1 |
| public int indexOf(String str,int fromIndex) | 从指定位置由前向后查找指定字符串,找不到返回 -1 |
| public int lastIndexOf(int ch) | 由后向前查找指定字符串,找不到返回 -1 |
| public int lastIndexOf(String str,int fromIndex) | 从指定位置由后向前查找指定字符串,找不到返回 -1 |
| public boolean startsWith(String prefix) | 判断是否以指定字符串开始 |
| public boolean startsWith(String prefix,int toffset) | 从指定位置判断是否以指定字符串开始 |
| public boolean endsWith(String suffix) | 判断是否以指定字符串结尾 |
| 字符串替换 | |
| public String replaceAll(String regex,String replacement) | 使用指定内容串替换所有旧的内容 |
| public String replaceFirst(String regex,String replacement) | 替换第一个满足条件的旧内容 |
| 字符串截取 | |
| public String substring(int beginIndex) | 从指定位置开始截取字符串 |
| public String substring(int beginIndex,int endIndex) | 截取指定范围内的字符串 |
| 字符串拆分 | |
| public String[] split(String regex) | 将目标字符串按照给定字符串进行全部拆分 |
| public String[] split(String regex,int limit) |
按照给定字符串进行部分拆分,limit为拆分后返回的 字符数组长度,当数组长度等于limit的值时, 即使后面字符串满足拆分条件也不再进行拆分 |
| public String concat(String str) | 字符串连接,相当于“+” |
| public String toLowerCase() | 字符串转小写 |
| public String toUpperCase() | 字符串转大写 |
| public String trim() | 去掉字符串两端的空格,中间空格保留 |
| public int length() | 取得字符串长度 |
| public String intern() | 数据入池 |
| public boolean isEmpty() | 判断是否为空字符串(“”) |

浙公网安备 33010602011771号