String类
文章目录
- String类
- String的构造方法
- 求字符串的长度
- 字符串的比较
- 字符串的查找
- 转化
- 1、数字转化为字符串
- 2、字符与字符串
- 字符转化为字符串
- 字符串转化为字符
- 判断一个字符串是不是全是由数字组成的?
- 3、字节与字符串
- 将字节转化为字符串
- 将字符串转化为字节
- 将字符串转化为一个字符数组
- 格式化
- equals是否区分大小写的两种方法
- 比较两个字符串的大小
- 字符串的替换
- 字符串的截取
- 消去字符串的左右空格trim
- 修改字符的大小写
- 字符串的拆分
- 字符串拆分的一些特例
- 多次拆分
- 字符串常量池
- intern(手动入池)
- 请解释一下String两种对象实例化的区别
- 字符串的不可变性
- stringBuilder 和 StringBuffer
- StringBuilder和StringBuffer的区别
- String StringBuilder StringBuffer三者的区别
我们都知道,字符串是十分重要的的,为此,Java专门提供了一种String类。
String的构造方法
1、直接进行初始化
2、new String对象
3、通过char数组进行构造
public static void main(String[] args) {
String str1 = "stay";
String str2 = new String("stay");
char[] ch = {'s', 't', 'a', 'y'};
String str3 = new String(ch);
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
那么String类内部究竟是有哪些组成的呢?
查看源码并通过编译就可以知道
String类里面存储的是char类型的value数组以及hash
String是引用类型,里面存储的是地址
求字符串的长度
直接字符串名.length()
public static void main(String[] args) {
String str1 = "stay";
System.out.println(str1.length());
int[] arr = {1, 2, 3, 4};
System.out.println(arr.length);
}
需要注意的是,字符串里面的length是方法,要加上括号,而求数组的长度时,length是数组本身的属性,不需要加上括号
字符串的比较
1、字符串名==字符串名进行地址比较
2、equals()与equalsIgnoreCase()进行内容比较
3、compareTo()与compareToIgnoreCase()进行大小比较
public static void main(String[] args) {
String str1 = new String("hello");
String str2 = new String("Hello");
System.out.println(str1 == str2);//str里面存放的是地址,new了两个不一样的对象,地址一定就不一样,所以输出结果是false
System.out.println(str1.equals(str2));//equals比较的是字符串的内容
System.out.println(str1.equalsIgnoreCase(str2));//忽略大小写比较字符串的内容
System.out.println(str1.compareTo(str2));//调用compareTo方法,比较字符串的大小。要是str1大于str2就返回正数,否则返回负数
System.out.println(str1.compareToIgnoreCase(str2));//使用compareToIgnoreCase就会忽略大小写进行比较
}
字符串的查找
1、chaeAt()可以得到对应下标的字符
2、indexOf()用来会返回某个字符或者某个字符串首次出现的下标,要是找不到就返回-1
3、lastIndexOf从后往前找,先找到对应的字符再返回下标
public static void main(String[] args) {
String s1 = "stay hungry";
for (int i = 0; i < s1.length(); i++) {
char ch = s1.charAt(i);
System.out.print(ch + " ");
}
System.out.println();
int index = s1.indexOf('h');
System.out.println(index);//5
int index2 = s1.indexOf('g', 2);//从下标为2的位置开始查找
System.out.println(index2);//8
int index3 = s1.indexOf("hu");
System.out.println(index3);//5
}
public static void main(String[] args) {
String str = "ababaryth"
int n = str.lastIndexOf('t');
System.out.println(n);//7
int n2 = str.lastIndexOf('a', 3);//从abab开始向前找
System.out.println(n2);//2
}
转化
1、数字转化为字符串
public static void main(String[] args) {
String str = String.valueOf(123);
System.out.println(str);//123
}
另外,还可以将类也变成字符串
class Stu{
public int ID;
public Stu(int ID) {
this.ID = ID;
}
@Override
public String toString() {
return "Stu{" +
"ID=" + ID +
'}';
}
}
public class Test {
public static void main(String[] args) {
String str = String.valueOf(new Stu(12345));
System.out.println(str);
}
}
//Stu{ID=12345}
将字符串转化为数字(可以使用进制)
public static void main(String[] args) {
int a = Integer.valueOf("12", 8);//按照八进制进行转化
System.out.println(a);
//10
int b = Integer.parseInt("1234");
System.out.println(b);
//10
//Integer.valueOf和Integer.parseInt都是一样的
在常量池里面相同的字符串只会存在一份
字符 字节 字符串的关系
2、字符与字符串
字符转化为字符串
public static void main(String[] args) {
char[] val = {'a', 'b', 'c', 'd' };
String str = new String(val);
System.out.println(str);//abcd
String str2=new String(val,1,2);//偏移量是1,数量是2
System.out.println(str2);//bc
}
字符串转化为字符
public static void main(String[] args) {
String str1 = "hungry";
char ch = str1.charAt(3);//输出偏移量(offset)(下标)为3的字母
System.out.println(ch);//g
char[] val2 = str1.toCharArray();//将字符串变为数组
System.out.println(Arrays.toString(val2));
}
判断一个字符串是不是全是由数字组成的?
public static boolean IsNum(String str){
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);//获得数组的每一个元素
if (ch < '0' || ch > '9') {
return false;
}
}
return true;
}
//判断一个字符串中是不是全部都是数字
public static void main(String[] args) {
String str = "12345";
boolean flg = IsNum(str);
System.out.println(flg);
}
3、字节与字符串
将字节转化为字符串
public static void main(String[] args) {
byte[] e = {100, 101, 102,103};
String str = new String(e);
System.out.println(str);//defg
String str2 = new String(e,1,2);//偏移量(offset)为1,数量为2
System.out.println(str2);//ef
}
将字符串转化为字节
public static void main(String[] args) {
String str = "hello";
byte[] e = str.getBytes();
System.out.println(Arrays.toString(e));
}
将字符串转化为一个字符数组
public static void main(String[] args) {
String s1 = "stay hungry";
char[] ch = s1.toCharArray();
for (char x : ch) {
System.out.println(x);
}
}
//并不会改变s1,只是创建了一个新的对象
格式化
public static void main(String[] args) {
String s1 = String.format("%d %d %d", 2021, 5, 31);
System.out.println(s1);
}
//2021 5 31
下面就要介绍一下
equals是否区分大小写的两种方法
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "hello";
System.out.println(str1.equals(str2));//严格区分大小写
System.out.println(str1.equalsIgnoreCase(str2));//不进行大小写区分
}
比较两个字符串的大小
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "Hell";
System.out.println(str1.compareTo(str2));
}
//String里面已经重写了compareTo方法
//要是str1大于str2就返回正数,否则就返回负数
字符串的替换
public static void main(String[] args) {
String str1 = "Hellooth";
String str2 = str1.replace('H', 'k');//将所有的H替换成K
System.out.println(str2);
System.out.println("=======================");
String str3 = str1.replace("ll", "yy");
System.out.println(str3);
System.out.println("=======================");
String str4="shoopeoohe";
String str5=str4.replaceFirst("oo","uk");
System.out.println(str5);
}
//replace既可以替换单个字符,也可以替换字符串(替换所有的字符串)
//replaceFirst会替换第一次出现的单个字符或者字符串
字符串的截取
public static void main(String[] args) {
String str1 = "Hellooth";
String ret = str1.substring(1);//从下标为1处开始往后截取
System.out.println(ret);//ellooth
String ret2 = str1.substring(1,3);//左闭右开
System.out.println(ret2);//el
}
消去字符串的左右空格trim
public static void main(String[] args) {
String str1 = " He llooth ";
String ret = str1.trim();
System.out.println(ret);
}
//He llooth
//只能消去左右两侧的空格,但是不能消去字符串中间的空格
修改字符的大小写
public static void main(String[] args) {
String s1 = "hello";
String ret = s1.toUpperCase();
System.out.println(ret);
String s2 = "HELLO";
String ret2 = s2.toLowerCase();
System.out.println(ret2);
System.out.println("s2:" + s2);
}
//HELLO
//hello
//s2:HELLO 也就是说toUpperCase和toLowerCase并不会改变原来的字符串的值,它新创造了一个字符串
字符串的拆分
public static void main(String[] args) {
String s1 = "welcome to the world";
String[] ret = s1.split(" ");//以空格为拆分的标识
for (String x : ret) {
System.out.println(x);
}
}
//welcome
//to
//the
//world
这个split方法是有重载的,也可以传两个参数,第二个参数表示最多拆分成几组
public static void main(String[] args) {
String s1 = "welcome to the world";
String[] ret = s1.split(" ",3);
for (String x : ret) {
System.out.println(x);
}
}
//welcome
//to
//the world
字符串拆分的一些特例
- 字符"|“,”*“,”+“都得加上转义字符,前面加上”" .
- 而如果是" 斜杠 " ,那么就得写成"\" .
- 如果一个字符串中有多个分隔符,可以用"|"作为连字符.
public static void main(String[] args) {
String s1 = "123.45.1.1";
String[] ret= s1.split("\.");//两个斜杠表示真正的斜杠
for (String x : ret) {
System.out.println(x);
}
public static void main(String[] args) {
String s1 = "123\45\1\1";//要是只写一个斜杆就会被转义,所以就写成了两个斜杠
String[] ret= s1.split("\\");//四个斜杠其实就是两个真正的斜杠
for (String x : ret) {
System.out.println(x);
}
要是有多个分隔符,就可以使用 | 进行了分隔
public static void main(String[] args) {
String s1 = "zhangsan wangwu&lisi";
String[] ret = s1.split(" |&");//以空格和&进行分隔
for (String x : ret) {
System.out.println(x);
}
}
//zhangsan
//wangwu
//lisi
多次拆分