目录
4、 concat(); 把两个字符串拼接起来,获取一个新的字符串
5、★length(); 获取字符串的长度(其实就是获取字符串中 字符的个数
6、 ★charAt(); 根据给定的索引,获取对应位置的字符
10、 ★isEmpty(); 判断字符串是否为空(长度为0返回true,不为0返回false
11、 ★contains(); 判断字符串中是否包含 给定的字符串。
12、 endsWith(); 判断字符串是否以 给定的字符串 结尾。
13、 startsWith(); 判断字符串是否以 给定的字符串 开头。
14、 ★replace(); 用新内容替代旧内容,返回新的字符串
15、 toLowerCase(); //把字母都转成其对应的小写形式。
16、toUpperCase(); //把字母都转成其对应的大写形式。
20、★split(); //根据给定的内容,切割字符串,返回字符串数组
一.String类概述
- String是一个final类,不可被继承;代表不可变的字符序列。简称:不可变性。
- 字符串是常量,用双引号引起来表示。它们的值在创建之后不能更改
- String对象的字符内容是存储在一个字符数组value[]中的
二.String类的不可变性
- 当对字符串重新赋值时,需要重写指定内存区域赋值,不能使用原有的
- 当对现有的字符串进行连接操作时,需要重新指定内存区域赋值,不能使用原有的
- 通过字面量的方式(区别于new)给一个字符串赋值,此时的字符串值声明在常量池中
- 常量池中是不会存储相同内容的字符串的
三.创建字符串对象两种方式的区别
- 通过构造方法创建:通过 new 创建的字符串对象,每一次 new 都会申请一个内存空间,虽然字符串内容相同,但是地址值不同
- 直接赋值方式创建:以" "方式给出的字符串,只要字符序列相同(顺序和大小写),无论在程序代码中出现几次,JVM 都只会建立一个 String 对象,并在常量池池中维护
注意:
-
- 常量与常量的拼接结果在常量池。且常量池中不会存在相同内容的常量。
- 只要其中有一个是变量,结果就在堆中。
四、常用string方法介绍
1、==比较运算符
基本数据类型比较的是2个变量中的值是否相同。
引用数据类型比较的是2个对象的地址值是否相同。
2、equals方法
public boolean equals(String s) 比较两个字符串内容是否相同、区分大小写
比较两个字符串的内容是否相同,区分大小写,相同返回true,不相同返回false。
3、equalsIgnoreCase方法
public boolean equalsIgnoreCase (String anotherString)
:将此字符串与指定对象进行比较,忽略大小写。
小结:
-
比较字符串内容是否相等,区分大小写,需要使用String类的equals方法,千万不要用 == 比较
-
如果比较字符串内容是否相等,不区分大小写,需要使用String类的equalsIgnoreCase()方法
4、 concat(); 把两个字符串拼接起来,获取一个新的字符串
5、★length(); 获取字符串的长度(其实就是获取字符串中 字符的个数
6、 ★charAt(); 根据给定的索引,获取对应位置的字符
7、 ★indexOf(); 获取指定的字符 在字符串中 第一次出现的位置(索引),找不到返回-1
//int index = a1.indexOf('h'); 从头找,'h'第一次出现的位置
//int index = a1.indexOf('h',3); 从索引为3的元素开始往后找,'h'第一次出现的位置
8、lastIndexOf(); 获取指定的字符 在字符串中 最后一次出现的位置(索引),找不到返回-1
//int index = a1.lastIndexOf('h'); 从尾部找,'h'最后一次出现的位置
//int index = a1.lastIndexOf('h',3); 从索引为3的元素开始往前找,'h'最后一次出现的位置
9、 ★substring(); 截取字符串,返回新的字符串
//String newStr = a1.substring(2); //从给定索引,直接截取到字符串末尾
//String newStr = a1.substring(2,5); //包左不包右(前闭后开), 能取索引2的元素,不能取索引5的元素
10、 ★isEmpty(); 判断字符串是否为空(长度为0返回true,不为0返回false
11、 ★contains(); 判断字符串中是否包含 给定的字符串。
12、 endsWith(); 判断字符串是否以 给定的字符串 结尾。
13、 startsWith(); 判断字符串是否以 给定的字符串 开头。
14、 ★replace(); 用新内容替代旧内容,返回新的字符串
15、 toLowerCase(); //把字母都转成其对应的小写形式。
16、toUpperCase(); //把字母都转成其对应的大写形式。
17、toCharArray() // 把字符串转换为数组
18、getBytes() // 把字符串转换为字节数组
19、 ★trim(); //移除首尾空格。
20、★split(); //根据给定的内容,切割字符串,返回字符串数组
public class Test {
public static void main(String[] args) {
/*
★boolean isEmpty(); //判断字符串是否为空(长度为0返回true,不为0返回false)
★boolean contains(CharSequence s); //判断字符串中是否包含 给定的字符串。
endsWith(); //判断字符串是否以 给定的字符串 结尾。
startsWith(); //判断字符串是否以 给定的字符串 开头。
★replace(); //用新内容替代旧内容,返回新的字符串
toLowerCase(); //把字母都转成其对应的小写形式。
toUpperCase(); //把字母都转成其对应的大写形式。
toCharArray() // 把字符串转换为数组
getBytes() // 把字符串转换为字节数组
★trim(); //移除首尾空格。
★split(); //根据给定的内容,切割字符串,返回字符串数组
*/
// isEmpty(); //判断字符串是否为空(长度为0返回true,不为0返回false)
String str1 = null;// 空 表示什么都没有
String str2 = "";// 空字符串对象
String str3 = " ";// 有一个空格字符的字符串对象
//System.out.println(str1.isEmpty());// 报空指针异常,因为null不能调用方法
System.out.println(str2.isEmpty());// true
System.out.println(str3.isEmpty());// false
System.out.println("=========================");
String str = "itheima-itcast";
// boolean contains(CharSequence s); //判断字符串中是否包含 给定的字符串
// 需求:判断str字符串中是否包含itcast
System.out.println(str.contains("itcast"));// true
System.out.println(str.contains("itcasts"));// false
System.out.println("=========================");
// boolean endsWith(String subff); //判断字符串是否以 给定的字符串 结尾。
String fileName = "Test.java";
System.out.println(fileName.endsWith(".java"));// true
System.out.println(fileName.endsWith(".txt"));// false
// startsWith(); //判断字符串是否以 给定的字符串 开头。
System.out.println(fileName.startsWith("Test"));// true
System.out.println(fileName.startsWith("Demo"));// false
System.out.println("=========================");
// ★String replace(CharSequence target, CharSequence replacement); //用新内容替代旧内容,返回新的字符串
String message = "啊啊特朗普啊啊啊特朗普----特朗普";
// 需求: 把特朗普替换为***
String msg = message.replace("特朗普", "***");
System.out.println("原字符串:"+message);// 啊啊特朗普啊啊啊特朗普----特朗普
System.out.println("新字符串:"+msg);// 啊啊***啊啊啊***----***
System.out.println("=========================");
// toLowerCase(); //把字母都转成其对应的小写形式。
String str4 = "aBcD";
String s4 = str4.toLowerCase();
System.out.println("s4:"+s4);// abcd
// toUpperCase(); //把字母都转成其对应的大写形式。
String s5 = str4.toUpperCase();
System.out.println("s5:"+s5);// ABCD
// toCharArray() // 把字符串转换为数组
char[] chs = str4.toCharArray();
for (int i = 0; i < chs.length; i++) {
System.out.println(chs[i]);// a B c D
}
System.out.println("=========================");
// getBytes() // 把字符串转换为字节数组
byte[] bytes = str4.getBytes();
for (int i = 0; i < bytes.length; i++) {
System.out.println(bytes[i]);// 97,66,99,68
}
System.out.println("=========================");
// ★trim(); //移除首尾空格。
String username = " zhangsan ";
String name = username.trim();
System.out.println("==="+name+"===");
System.out.println("=========================");
// ★String[] split(String regex); //根据给定的内容,切割字符串,返回字符串数组
// 基本用法: 传入一个普通的字符串,没有特殊含义
String str5 = "itcast,itheima,boxuegu";
// 指定按照逗号进行切割字符串
String[] arr = str5.split(",");
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
System.out.println("=========================");
// 高级用法: 传入有特殊含义的字符串,其实就是正则表达式
String str6 = "abc bcd cba nba";
String[] arr2 = str6.split(" +");
System.out.println(arr2.length);
for (int i = 0; i < arr2.length; i++) {
System.out.println(arr2[i]);
}
System.out.println("=========================");
String str7 = "abc.cba.nba.bac";
String[] arr3 = str7.split("\\.");
System.out.println(arr3.length);
for (int i = 0; i < arr3.length; i++) {
System.out.println(arr3[i]);
}
}
}