考完六级,搬完新家,有10天没学习java,要把java捡起来了呀,必须得加快点进度。
加油!
字符串相关的类:String
-
String:字符串。使用“”引起来表示
-
String声明为final的,不可被继承
-
String实现了Serializable接口:表示字符串是支持序列化的;实现了Comparable接口:表示String可以比较大小
-
String内部定义了final char[] value 用于存储字符串数据
-
String:代表不可变的字符序列。简称:不可变性
- 当对字符串重新赋值、对现有的字符串进行连接操作、调用String的replace()方法修改指定字符或字符串时…只要修改了字符串,需要重新指定内存区域赋值,不能使用原有的value进行赋值。
String a1="abc";//字面量的定义方式
String a2="abc";
System.out.println(a1==a2);//true
- 通过字面量的方式(区别于new)给一个字符串赋值,此时的字符串值声明在字符串常量池中。
- 字符串常量池中是不会存储相同内容的字符串的。
String的实例化方式
String a1="abc";//字面量的定义方式
String a2="abc";
String a3=new String("abc");
String a4=new String("abc");
System.out.println(a1==a2);//true
System.out.println(a1==a3);//False
System.out.println(a1==a4);//False
System.out.println(a3==a4);//False
- 通过字面量定义的方式:数据声明在方法区中的字符串常量池中。
- 通过new + 构造器的方式,是数据在堆空间中开辟空间以后对应的地址。
System.out.println(p1.name.equals(p2.name));//true
System.out.println(p1.name== p2.name);//true,通过字面量方式定义,数据都在常量池
class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public Person() {
}
}
String的不同拼接操作
- 常量与常量的拼接结果在常量池。且常量池中不会存在相同内容的常量。
- 只要其中有一个是变量,结果就在堆中。
- 如果拼接的结果调用intern()方法,返回值就在常量池中。
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;
String s8 = s7.intern();
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(s3==s8);//true
System.out.println(s5==s6);//false
System.out.println(s5==s7);//false
System.out.println(s6==s7);//false
final String s9 ="Hello";//s9:常量
String s10 =s9+"World";
System.out.println(s3==s10);//true
①
面试题:String s = new String(“abc”);方式创建对象,在内存中创建了几个对象?
答案:两个,一个是堆空间中new结构,另一个是char[]对应的常量池中的数据:“abc”
②
public class StringTest03 {
public static void main(String[] args) {
String str = "good";
char[] ch={'t','e','s','t'};
new StringTest03().change(str,ch);
System.out.println(str);//good String的不可变性,
System.out.println(ch);//best
}
public void change(String str,char ch[]){ //引用传递
str="test ok";
ch[0]='b';
}
}
String常用方法
第一波:
- 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(不包含)的一个子字符串;(左闭右开)。有些方法是从某个位置开始取几个。
public class StringMethodTest01 {
public static void main(String[] args) {
String s1="Hello World";
System.out.println(s1.length());//11 空格也算; 1. int length();返回字符串的长度: return value.length
System.out.println(s1.charAt(0));//H 2. char charAt(int index);返回某索引处的字符:return value[index]
System.out.println(s1.charAt(10));//d
System.out.println(s1.isEmpty());//false 3. boolean isEmpty();判断是否为空字符:return value.length == 0
String s2=s1.toLowerCase();// 4. String toLowerCase();使用默认语言环境,将String中的所有字符转换为小写
String s3=s1.toUpperCase();// 5. String toUpperCase();使用默认语言环境,将String中的所有字符转换为大写
System.out.println(s1);//Hello World
System.out.println(s2);//hello world
System.out.println(s3);//HELLO WORLD
String s4=" He llo World ";
String s5=s4.trim(); // 6. String trim();返回字符串的副本,忽略前导空白和尾部空白
System.out.println("---"+s4+"---");//--- He llo World ---
System.out.println("---"+s5+"---");//---He llo World---
System.out.println(s1.equals(s2));//false 7. boolean equals(Object obj);比较字符串的内容是否相同
System.out.println(s1.equalsIgnoreCase(s2));//true 8. boolean equalsIgnoreCase(String anotherString);与equals方法类似,忽略大小写
String s6 ="Hello";
String s7 =" World";
System.out.println(s6.concat(s7));//Hello World 9. String concat(String str);将指定字符串连接到此字符串的结尾。等价于用"+"
String s8="abc";
String s9=new String("abd");
System.out.println(s8.compareTo(s9));//-1 涉及到字符串排序。10. int compareTo(String anotherString);比较两个字符串的大小
String s10="陈浩贤超级爱泳琳猪";
System.out.println(s10.substring(1));//浩贤超级爱泳琳猪
System.out.println(s10.substring(1, 8));//浩贤超级爱泳琳
// 11. String substring(int beginIndex);返回一个新的字符串,它是此字符串的从beginIndex开始截取到最后的一个子字符串
// 12. 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);返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。
public static void main(String[] args) {
String s1="Hello World";
System.out.println(s1.endsWith("ld"));// true
System.out.println(s1.startsWith("He"));//true
System.out.println(s1.startsWith("he"));//false,区分大小写
System.out.println(s1.startsWith("ll", 2));//true
//1. boolean endsWith(String suffix);测试此字符串是否以指定的后缀结束
//2. boolean startsWith(String prefix);测试此字符串是否以指定的前缀开始
//3. boolean startsWith(String prefix,int toffset);测试此字符串从指定索引开始的子字符串是否以指定的前缀开始
String s2="lo";
System.out.println(s1.contains(s2));//true
//4. boolean contains(CharSequence s);当且仅当此字符串包含指定的char值序列时,返回true
System.out.println(s1.indexOf(s2));//3
System.out.println(s1.indexOf("lo w"));//-1
System.out.println(s1.indexOf("or",5));//7
//5. int indexOf(String str);返回指定子字符串在此字符串中第一次出现处的索引
//6. int indexOf(String str,int fromIndex);返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始
String s3="hellorworld";
System.out.println(s3.lastIndexOf("or"));//7
System.out.println(s3.lastIndexOf("or",6));//4
// 7. int lastIndexOf(String str);返回指定子字符串在此字符串中最后边出现处的索引
// 8. int lastIndexOf(String str,int fromIndex);返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。
//注:indexOf和lastIndexOf方法如果未找到都是返回-1
}
第三波
- 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个,如果超过了,剩下的全部都放到最后一个元素中。
public static void main(String[] args) {
String s1="陈浩贤非常爱梁泳琳";
System.out.println(s1.replace("陈", "帅"));
System.out.println(s1.replace("非常", "超级"));
// 1. String replace(char oldChar,char newChar);返回一个新的字符串,它是通过用newChar替换此字符串中出现的所有oldChar得到的。
// 2. String replace(CharSequence target,CharSequence replacement);使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。
String s2="12hello23world41java241mysql1241";
System.out.println(s2.replaceAll("\\d+", ",").replaceAll("^,|,$",""));//hello,world,java,mysql
// 3. String replaceAll(String regex,String replacement);使用给定的replacement替换此字符串所有匹配给定的正则表达式的子字符串。
// 4. String replaceFirst(String regex, String replacement);使用给定的replacement替换此字符串匹配给定的正则表达式的第一个子字符串。
String s3="12345";
String s4="0760-88888888";
System.out.println(s3.matches("\\d+"));//判断字符串中是否全部由数字组成。
System.out.println(s4.matches("0760-\\d{8}"));
// 5. boolean matches(String regex);告知此字符串是否匹配给定的正则表达式。
String s5="hello|world|java|beckham|unlia";
String[] strs = s5.split("\\|");
for (int i = 0; i <strs.length; i++) {
System.out.println(strs[i]);
}
String[] strs2=s5.split("\\|",3);
for (int i = 0; i < strs2.length; i++) {
System.out.println(strs2[i]);
}
// 6. String[] split(String regex);根据给定正则表达式的匹配拆分此字符串
// 7. String[] split(String regex,int limit);根据匹配给定的正则表达式来拆分此字符串,最多不超过limit个,如果超过了,剩下的全部都放到最后一个元素中。
}
String与其他类型之间的转换
回顾:String与基本数据类型和包装类的转换
//String -->基本数据类型、包装类:调用包装类的静态方法:parseXXX(String str)
//基本数据类型、包装类 --> String:调用String重载的valueOf(xxx)
String str ="123";
int num = Integer.parseInt(str);//得到包装类后拆箱
String s = String.valueOf(num);
String s2=num+"";
String与char[] 之间的转换
//String-->char[]:调用String的toCharArray()
//char[]-->String:调用String的构造器
String str1="abc123";
char[] chars1 = str1.toCharArray();
for (int i = 0; i <chars1.length; i++) {
System.out.println(chars1[i]);
}
char[] chars2=new char[]{'h','e','l','l','o'};
String str2 = new String(chars2);
System.out.println(str2);
String与byte[] 之间的转换
- 编码:字符串–>字节(看得懂—>看不懂的二进制数据)
- 解码:编码的逆过程,字节—>字符串(看不懂的二进制数据—>看得懂)
- 说明:解码时:要求解码使用的字符集必须与编码时使用的字符集一致,否则会出现乱码。
//String与byte[]之间的转换
//编码:String --> byte[]:调用String的getBytes();
//解码:byte[] --> String:调用String的构造器
//编码:字符串-->字节(看得懂--->看不懂的二进制数据)
//解码:编码的逆过程,字节--->字符串(看不懂的二进制数据--->看得懂)
//说明:解码时:要求解码使用的字符集必须与编码时使用的字符集一致,否则会出现乱码。
String s3="我爱泳琳";
byte[] bytes = s3.getBytes();//使用默认的字符集,进行编码。
System.out.println(Arrays.toString(bytes));
byte[] gbks = s3.getBytes("gbk");//使用gbk字符集进行编码
System.out.println(Arrays.toString(gbks));
String s4 = new String(bytes);//使用默认的字符集,进行解码
System.out.println(s4);
String s5 = new String(gbks);
System.out.println(s5);//出现乱码,因为编码集和解码集不一致
String s6 = new String(gbks, "gbk");
System.out.println(s6);//没有出现乱码,因为编码集和解码集一致