String类
java.lang.String类使用
(一)概述
String 字符串,使用一堆""表示
-
String声名为final 不可被继承
-
string实现了serializable 接口也:表示字符串是支持序列化的,
-
string内部定义l final char[] value 用于存储字符
-
string代表不可变的字符序列
4.1 当对字符串重新赋值时,需要重写指定内存区域赋值,不能使用原有的value进行赋值。
4.2当对现有的字符串进行连接操作时,也需要重新指定内存区域赋值,不能使用原有的value进行赋值。
4.3 1. 当调用String的 replace() 方法修改指定字符或字符串时,也需要重新指定内存区域赋值,不能使用原有的value进行赋值。
-
通过字面量的方式(区别与new给一个字符串赋值 此时字符串值声名在字符串常量子中)
-
字符串常量池中是不会存储相同内容(使用String类的equals()比较,返回true)的字符串的)。
(二)string的特征
String类:代表字符串。Java程序中的所有字符串字面值(如"abc")都作为此类的实例实现。 String是一个final类,代表不可变的字符序列。 字符串是常量,用双引号引起来表示。它们的值在创建之后不能更改。 String对象的字符内容是存储在一个字符数组vaue[]中的。
1.2 代码举例
String s1 = "abc";//通过字面量的定义方式
String s2 = "def";
s1 = "hello";
System.out.println(s1 == s2); //false 比较s1与s2的地址值
System.out.println(s1);//hello
System.out.println(s2);//def
System.out.println("-----------------------");
String s3 = "abc";
s3 += "def";
System.out.println(s3);//abcdef
System.out.println(s2);//def
System.out.println("-----------------------");
String s4 ="test";
String s5 = s4.replace("t","b");
System.out.println(s4);//test
System.out.println(s5);//besb
1.3图示解析
2 string实例化
- 方式一:通过字面量定义的方式
- 方式二:通过new + 构造器的方式
面试题:
String s = new String("abc"); 方式创建对象,在内存中创建了几个对象?
两个:一个是堆空间中new结构,另一个是char[]对应的常量池中的数据:"abc
String str1=“abc”; 与 String str2= new String(“abc”); 的区别?
3. 字符串拼接方式赋值对比
3.1说明:
- 常量与常量的拼接结果在常量池。且常量池中不会存在相同内容的常量。
- 只要其中一个是变量,结果就在堆中。
- 如果拼接的结果调用
intern()方法,返回值就在常量池中
3.2 代码示例
String s2 = "hadoop";
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);//false
System.out.println(s3 == s6);//false
System.out.println(s3 == s7);//false
System.out.println(s5 == s6);//false
System.out.println(s5 == s7);//false
System.out.println(s6 == s7);//false
String s8 = s6.intern();//返回值得到的s8使用的常量值中已经存在的“javaEEhadoop”
System.out.println(s3 == s8);//true
****************************
String s1 = "javaEEhadoop";
String s2 = "javaEE";
String s3 = s2 + "hadoop";
System.out.println(s1 == s3);//false
final String s4 = "javaEE";//s4:常量
String s5 = s4 + "hadoop";
System.out.println(s1 == s5);//true
5 string常用类
5.1字符串操作
- length 返回字符串长度
- charAt() 返回索引字符
- isEmpty() 是否为空
- Tolowercse 大转小
- touppercase 小转达
- euqeal 是否相等
- concat 连接
[判断字符]
-
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):返回指定子字符串在此字符串中最后一次出现处的索引,从
5.3 字符串操作方法
-
替换:
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个,如果超过了,剩下的全部都放到最后一个元素中。