字符串
1、字符串的特点:
1)、字符串的内容永不可变。【注意】
2)、字符串可以共享使用。
3)、字符串相当于char[] (char型数组),但底层原理是byte[] (字节数组)
2、字符串的4种创建方式(一种直接创建,三种构造方法)
1)、public String() 创建一个空白字符串
使用格式
public static void main(String[] args){
String nums = new String();
System.out.println(nums);
}
运行结果:(空白)
2)、public static String (char[] array) 根据创建的字符数组,将其转化为字符串
使用格式
public static void main(String[] args){
char ch = {'z', 'p', 'h'};
String chars = new String(ch); //将数组转化为字符串
System.out.println(chars);
}
运行结果:
zph
3)、public String(byte[] array):根据字节数组的内容来创建对应的字符串
使用格式
public static void main(String[] args) {
byte[] array = {65,66,67};
String one = new String(array);
//字节数组转化为字符串将数字转化成对应Ascii的字母
System.out.println(one);
}
打印结果:
ABC
4)直接创建
使用格式
String name = "阿sir";
3、字符串常量池:
字符串常量池:程序当中直接写上双引号的字符串,就在字符串常量池中;
对于基本类型来说, == 进行的是数值的比较。
对于引用类型来说, == 进行的是地址的比较。、
字符串常量池的内存图
![CEZ11O{SANUBCHDRP18SXJ](C:\Users\29455\Desktop\CEZ11O{SAN
UBCHDRP18SXJ.png)
4、字符串转换的相关方法:
1、public char[] toCharArray() 将字符串转化为数组
具有返回值返回值为拆分之后的数组,需要创建一个新的数组来接收拆分之后的数组
使用格式:
String name = "alasijia";
char[] nameArray = name.toCharArray(); //将字符串name拆分成字符保存在字符数组nameArray中
System.out.println(nameCharry[0]); //打印数组中索引为0的元素 --> a
System.out.println(nameCharry.length) //打印新的数组的长度 --> 8
//注意:集合中的长度用size获取
2、public byte[] getBytes() 获得当前字符串底层的字节数组
使用格式
String name = "abc";
byte[] s = name.getBytes();
System.out.println(s[0]); // --> a
System.out.println(s[1]); // --> b
System.out.println(s[2]); // --> c
3、public String replace(oldchar, newchar) 用后面的字符或者字符串替代原来字符串中的旧的字符串 返回一个新的字符串
使用格式
String str = "alasijia";
String newstr = str.replace(a, *); //用 “*” 代替字符串中所有的 a
System.out.println(str); //打印原字符串
System.out.println(newstr); //打印新的字符串
运行结果
alasijia
*l*siji* //代替之后的效果
5、字符串内容的比较方法
**1、public boolean equals(Obj) **
参数可以是任何对象,只有当内容相同时参会返回ture,否则返回false
注意:
1、此种方法区分字符串的大小写!
2、equals具有对称性,也就是a.equals(b)和b.equals(a)效果一样。
3、如果比较双方是一个常量字符串和一个变量字符串,推荐把常量字符串写在前面。
使用格式
public static void main(String[] args) {
String one = "zph";
String two = "zph";
System.out.println(one.equals(two));
System.out.println("zph".equals(two));
System.out.println("Zph".equals(one));
}
运行结果:
true
true
false
2、public boolean equalsIgnoreCase(Obj)
和equals用法相同但是不区分大小写既 “abC” 和 "ABc"相同
public static void main(String[] args) {
String one = "ZPH";
String two = "zph";
System.out.println(one.equalsIgnoreCase(two));
System.out.println("zPh".equalsIgnoreCase(two));
System.out.println("zPH".equalsIgnoreCase(two));
}
运行结果:
true
true
true
6、字符串获取的相关方法
1、获取字符串的长度 public int legth();
注意:空格也会被计算在字符串数字里面
String str = "HelloWorld";
int length = str.length();
System.out.println(length);
运行结果
10
2、字符串的拼接 public String concat(String str)
将当前字符串和参数字符串拼接成新的字符串并返回新的字符串
注意:是将括号里面的字符串拼接在前面的字符串的后面
String str1 = "Hello";
String str2 = "World!";
String str3 = str1.concat(str2); //将str2 连接在str1的后面
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
运行结果
Hello
World!
HelloWorld!
3、查找参数字符串在原字符串中的位置 public String indexOf(String str)
返回值是下标
注意:
区分大小
如果找到,返回值是参数字符串的索引
如果没有找到,则返回-1
String str = "HelloWorld";
int index = str.indexOf("llo"); //参数字符串为"llo",在原字符串中查找
int index1 = str.indexOf("www");
System.out.println(index);
System.out.println(index1);
运行结果
2
-1
4、获得指定索引的字符元素public char charAt(int index)并返回索引位置对应的元素
需要声明对应类型的返回值接收索引查找到的字符
String str = "HelloWorld!";
char ch1 = str.charAt(1);
char ch2 = str.charAt(2);
char ch3 = str.charAt(3);
System.out.println(ch1);
System.out.println(ch2);
System.out.println(ch3);
运行结果
e
l
l
7、字符串的截取方法
public String.substring(int index) 截取下标index之后所有的字符串
public String.substring(int Range1, int Range2) [Range1, Range2) 截取范围
具有返回值,返回值为截取的新的字符串
8、字符串的分割public String.split(参数)
注意:以参数为分割点,参数也会被分割,从而参数不会被打印