String
介绍
String字符串的底层是字符数组
注意 : 1.字符串一旦确定,就不能修改
2.为了提高字符串的效率,java虚拟机使用了一种"缓存技术"----字符串常量池
创建一个字符串对象时,例如字符串"abc",先看字符串常量池中是否已有字符串"abc",如果 没有,就创建一个.如果字符串常量池中有"abc",就不再创建
构造方法
//字面量直接创建字符串
String s1 = "abc";
//通过创建对象的方式创建字符串
String s2 = new String("abc");
//通过字节数组的方式创建字符串
byte[] bytes1 = {97,98,99,100};
String s3 = new String(bytes1,0,3); //从第一位开始 长度为3
String s4 = new String(bytes1); //全部输出为字符串
System.out.println("s1是 "+s1); //s1是 abc
System.out.println("s2是 "+s2); //s2是 abc
System.out.println("s3是 "+s3); //s3是 abc
System.out.println("s4是 "+s4); //s4是 abcd
常用的方法
1) charAt(int index) 返回值为char型 返回字符串中对应索引的字符
String str1 = "abcdefghi";
char c1 = str1.charAt(2);
System.out.println(c1);
2) endWith(Sring str) 返回值为boolean型 判断某个字符串是否以指定字符串结尾
String str2 = "abcdefghi";
System.out.println(str2.endsWith("hi")); //true
System.out.println(str2.endsWith("aab")); //false
3) getBytes() 返回值为byte[] 把该字符串转换为字节数组并返回
String str3 = "abcdefghi";
byte b1[] = str3.getBytes();
System.out.println(b1[2]);
4) toCharArray() 返回值为char[] 把字符串转换为字符数组并返回
String str4 = "abcdefghi";
char c2[] = str4.toCharArray();
System.out.println(c2[4]);
5) indexOf(String str) 返回值为int型
String str5 = "abcdefghi";
System.out.println(str5.indexOf("defg"));
6) lastIndexOf(String str) 返回值为int型
String str6 = "abcdefgcdehcdehi";
System.out.println(str6.lastIndexOf("cde"));
7) length() 返回值为int型 获取字符串的长度
String str7 = "abcdefghi";
System.out.println(str7.length());
8) replace(String a,String b) 返回值为String型
String str8 = "aacddffoo5acca5ac49aacdef5ac4d8";
System.out.println(str8.replace("ac","$$"));
9) split(String str) 返回值为String[] 以指定的字符串进行分割,返回字符串数组
String str9 = "2024-01-15";
String s9[] = str9.split("-");
for(int i = 0;i < s9.length;i++)
System.out.print(s9[i]+" ");
10) subString(int index) 返回值为String型 截取字符串,获取指定索引开始(包含起始索引)到结束的字符串
String str10 = "abcdefghi8989222";
System.out.println(str10.substring(7));
//左闭右开
System.out.println(str10.substring(5,9));
11) toUpperCase() 返回值为String型 将原字符串的字母全部转为大写
String str11 = "abciKoqPKL";
System.out.println(str11.toUpperCase());
12) toLowerCase() 返回值为String型 将原字符串中的所有字母全部转为小写
String str12 = "DFA499iFDAIodawFDAW";
System.out.println(str12.toLowerCase());
13) trim() 返回值为String型 删除字符串左右两边的空格
String str13 = " 你好 888666 世界美好 ";
System.out.println(str13.trim());
14) equals() 比较两个字符串是否相同
System.out.println(str12.equals(str2));
15) 不区分大小写比较两个字符串是否相同
String str14 = "abCDeFgH";
String str15 = "ABcdEFgH";
System.out.println(str14.equalsIgnoreCase(str15));
StringBuffer和StringBuilder
介绍
底层都是字符数组,但是数组可变长,当需要频繁拼接字符串时,可以使用StringBuffer和StringBuilder
二者的区别 StringBuffer : 线程安全,在多线程环境下不会出现问题,常用于类中
StringBuilder : 非线程安全,在多线程环境下.可能会出现问题,常用于方法中
使用
// 创建对象
// StringBuilder sb = new StringBuilder("a");
StringBuilder sb = new StringBuilder();
// 尾部添加
sb.append("a");
sb.append("b").append("c").append("d");
// 添加到指定位置
sb.insert(1, "x");
// 反转
sb.reverse();
// 添加元素个数
System.out.println(sb.length());
// 当前容量
System.out.println(sb.capacity());
// 转换为String类型
String string = sb.toString();
System.out.println(string);
包装类
介绍
将基本数据类型封装成对象,便可以对其进行更多的操作
使用
//基本类型
byte b1 = 22;
//包装类型
Byte b2 = new Byte(b1);
Byte b3 = new Byte(b1);
System.out.println(b2);
System.out.println(b2.equals(b3));
boolean flag1 = true;
Boolean flag2 = new Boolean(flag1);
func1(b2);
func1(b3);
func1(flag2);
}
public static void func1(Object obj){
System.out.println(obj);
}
Integer
使用
//最大值
System.out.println(Integer.MAX_VALUE);
//最小值
System.out.println(Integer.MIN_VALUE);
System.out.println(Long.MAX_VALUE);
System.out.println(Short.MAX_VALUE);
System.out.println(Byte.MIN_VALUE);
//创建对象
//Integer t1 = new Integer(12);
//传入纯数字的字符串
//Integer t2 = new Integer("888");
相互转换
//int 到 Integer
Integer i1 = Integer.valueOf(10);
//Integer 到 int
int i2 = i1.intValue();
//String 到 Integer
Integer i3 = Integer.valueOf("666");
//Integer 到 String
String s1 = i3.toString();
//int 到 String
String s2 = 8848+"";
//String 到 int
int i4 = Integer.parseInt("520");
常用方法
//创建对象 把int类型封装为integer类型
Integer i1 = new Integer(10);
//通过intValue 把包装类转换为基本类型
int i2 = i1.intValue();
System.out.println(i2);
//$$$ static int parseInt(String str) 将纯数字的字符串转换为int型
int i3 = Integer.parseInt("66377");
System.out.println(i3);
double d1 = Double.parseDouble("488666");
System.out.println(d1);
// static String toBinaryString(int value) 将int型数字转换为字符串类型的二进制
String s1 = Integer.toBinaryString(15);
System.out.println(s1);
//转八进制
System.out.println(Integer.toOctalString(15));
//转十六进制
System.out.println(Integer.toHexString(16));
自动装箱与自动拆箱
//装箱 基本类型到引用类型
//拆箱 引用类型到基本类型
//1.5之前的装箱
Integer i1 = new Integer(3);
i1 = Integer.valueOf(3);
//1.5以后的装箱
Integer i2 = 13;
//1.5之前的拆箱
int i3 = i2.intValue();
//1.5之后的拆箱
int i4 = i2;