目录
1.概述
1.String用来声明字符串,声明内容用“”装入;
2.String不可被继承,具有不可变性;
3.String原码声明为final类型的,不可被继承;
4.String支持序列化,并且可以比较大小;
5.String底层用的char[]数组;
6.String值声明在字符串常量池中。
2.String的不可变性
1.String声明在常量池中,在对此类型进行操作时,不会改变原来声明值的内容;
代码举例
package arrar;
public class text20 {
public static void main(String[] args){
String s1 = "abc";
String s2 = "abc";
System.out.println(s1 == s2);
}
}
输出结果:
true
Process finished with exit code 0
3.String实例化的不同方式
1.
方式一:通过字面量定义的方式
方式二:通过new + 构造器的方式
注意:实例化方式不同,声明的区域不同
代码举例:
package arrar;
public class text20 {
public static void main(String[] args){
String s1 = "abc";
String s2 = "abc";
String s3 = new String("java");
String s4 = new String("java");
System.out.println(s1 == s2);
System.out.println(s3 == s4);
}
}
运行结果:
true
false
Process finished with exit code 0
4.字符串拼接方式赋值的对比
1.常量与常量的拼接结果在常量池,且常量池中不会存在相同内容的常量。
2.拼接只要其中一个是变量,结果就在堆中。
3.如果拼接的结果调用intern()方法,返回值就在常量池中
代码举例:
package arrar;
public class text20 {
public static void main(String[] args){
String s1 = "hello";
String s2 = "world";
String s3 = "helloworld";
String s4 = "hello" + "world";
String s5 = s1 + "world";
String s6 = "hello" + s2;
String s7 = s1 + s2;
System.out.println(s3 == s4);
System.out.println(s3 == s5);
System.out.println(s3 == s6);
System.out.println(s3 == s7);
System.out.println(s5 == s6);
System.out.println(s5 == s7);
System.out.println(s6 == s7);
}
}
运行结果:
true
false
false
false
false
false
false
Process finished with exit code 0
5.常用方法
int length():返回字符串的长度: return value.length
char charAt(int index): 返回某索引处的字符return value[index]
boolean isEmpty():判断是否是空字符串:return value.length == 0
String toLowerCase():使用默认语言环境,将 String 中的所字符转换为小写
String toUpperCase():使用默认语言环境,将 String 中的所字符转换为大写
String trim():返回字符串的副本,忽略前导空白和尾部空白
boolean equals(Object obj):比较字符串的内容是否相同
boolean equalsIgnoreCase(String anotherString):与equals方法类似,忽略大小写
String concat(String str):将指定字符串连接到此字符串的结尾。 等价于用“+” int
compareTo(String anotherString):比较两个字符串的大小
String substring(int beginIndex):返回一个新的字符串,它是此字符串的从beginIndex开始截取到最后的一个子字符串。
String substring(int beginIndex, int endIndex) :返回一个新字符串,它是此字符串从beginIndex开始截取到endIndex(不包含)的一个子字符串。
boolean endsWith(String suffix):测试此字符串是否以指定的后缀结束 boolean startsWith(String prefix):测试此字符串是否以指定的前缀开始
boolean startsWith(String prefix, int toffset):测试此字符串从指定索引开始的子字符串是否以指定前缀开始
boolean contains(CharSequence s):当且仅当此字符串包含指定的 char 值序列时,返回 true int
indexOf(String str):返回指定子字符串在此字符串中第一次出现处的索引
int indexOf(String str, int fromIndex):返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始
int lastIndexOf(String str):返回指定子字符串在此字符串中最右边出现处的索引 int
lastIndexOf(String str, int fromIndex):返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索
替换:
String replace(char oldChar, char newChar):返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所 oldChar 得到的。
String replace(CharSequence target, CharSequence replacement):使用指定的字面值替换序列替换此字符串所匹配字面值目标序列的子字符串。
String replaceAll(String regex, String replacement):使用给定的 replacement 替换此字符串所匹配给定的正则表达式的子字符串。
String replaceFirst(String regex, String replacement):使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。
匹配:
boolean matches(String regex):告知此字符串是否匹配给定的正则表达式。
切片:
String[] split(String regex):根据给定正则表达式的匹配拆分此字符串。
String[] split(String regex, int limit):根据匹配给定的正则表达式来拆分此字符串,最多不超过limit个,如果超过了,剩下的全部都放到最后一个元素中。
6.String与其它结构的转换
1 与基本数据类型、包装类之间的转换 String --> 基本数据类型、包装类
调用包装类的静态方法:parseXxx(str) 基本数据类型、包装类 --> String:调用String重载的valueOf(xxx)
代码举例:
package arrar;
public class text20 {
public static void main(String[] args){
String s1 = "123";
String s2 = String.valueOf(s1);
String s3 = s2 + "";
}
}
2 与字符数组之间的转换 String --> char[]
代码举例
package arrar;
public class text20 {
public static void main(String[] args){
String s1 = "helloworld";
char[] s2 = s1.toCharArray();
for(int i = 0; i < s2.length;i++){
System.out.println(s2[i]);
}
}
}
运行结果
h
e
l
l
o
w
o
r
l
d
Process finished with exit code 0
3 与字节数组之间的转换
package arrar;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
public class text20 {
public static void main(String[] args) throws UnsupportedEncodingException {
String s1 = "helloworld";
byte[] bytes = s1.getBytes();//使用默认的字符集,进行编码。
byte[] gbks = s1.getBytes("gbk");//使用gbk字符集进行编码。
System.out.println(Arrays.toString(gbks));
}
}
运行结果:
[104, 101, 108, 108, 111, 119, 111, 114, 108, 100]
Process finished with exit code 0