目录
一、包装类
1.包装类是什么
类图:
2.装箱和拆箱
/**
* @author: 程序员飞扬
* @description:
*/
public class WrapperType {
public static void main(String[] args) {
int n = 10;
Integer integer = new Integer(n);//手动装箱
Integer integer1 = Integer.valueOf(n);//第二种手动装箱方式
Integer integer2 = new Integer(90);
int i = integer2.intValue();//手动拆箱
//jdk5之后自动
int m = 10;
Integer m2 = m;//自动装箱,底层用的还是Integer.valueOf()
Integer m3 = new Integer(666);
int m4 = m3;//自动拆箱
}
}
3.经典面试题
4.包装类和String类的转换
/**
* @author: 程序员飞扬
* @description:包装类型和String类型的相互转换
*/
public class WrapperToString {
public static void main(String[] args) {
//包装类型 -> String类型
Integer i =10;
String s = i.toString();//方式1
String s1 = String.valueOf(i);//方式2
String s2 = i + "";//方式3
//String -> 包装类
Integer integer = new Integer(s);//构造器方式
int i1 = Integer.parseInt(s);//自动装箱方式
}
}
6.包装类方法
常用方法(还有很多,用到查即可)
7.面试题
1)
源码:
2)
二、String类
1.String结构剖析
2.String创建剖析
3.测试题
3.字符串的特性
4.String常用方法
第一组方法:
/**
* @author: 程序员飞扬
* @description:
*/
public class StringMethod01 {
public static void main(String[] args) {
//1. equals 比较是否相等
String str1 = "hello";
String str2 = "HELLO";
System.out.println(str1.equals(str2));//false
//2. equalsIgnoreCase 忽略大小写判断
System.out.println(str1.equalsIgnoreCase(str2));//true
//3. length 获取字符个数,字符串长度
String str3 = "飞扬";
System.out.println(str3.length());//2
//4. indexOf 获取字符在字符串对象中第一次出现的索引
String str4 = "feiyang";
System.out.println(str4.indexOf("y"));//3
//5. lastIndexOf 获取字符在字符串对象中最后一次出现的索引
String str5 = "gvd@sd@noln";
System.out.println(str5.lastIndexOf("@"));//6
//6. subString 截取指定范围的字符串
String str6 = "helloworld";
System.out.println(str6.substring(5));//world
System.out.println(str6.substring(5,8));//wor
//7. trim 去掉前后空格
String str7 = " 789 ";
System.out.println(str7.trim());//789
//8. charAt 获取某索引处的字符
String str8 = "bilibili";
System.out.println(str8.charAt(2));//l
}
}
第二组方法:
/**
* @author: 程序员飞扬
* @description:
*/
public class StringMethod02 {
public static void main(String[] args) {
//1. toUpperCase 转换为大写
String str1 = "hello";
System.out.println(str1.toUpperCase());//HELLO
//2. toLowerCase 转换为小写
String str2 = "HELLO";
System.out.println(str1.toLowerCase());//hello
//3. concat 拼接字符串
String str3 = "红楼梦";
String concat = str3.concat("西游记").concat("三国演义").concat("水浒传");
System.out.println(concat);//红楼梦西游记三国演义水浒传
//4. replace 替换字符串中的字符
String str4 = "helloworld";
System.out.println(str4.replace("hello","hi"));//hiworld
//replaceAll 替换字符串中所有匹配的字符
System.out.println(str4.replaceAll("l","L"));//heLLoworLd
//5. split 分隔符,对于某些字符需要转义,比如| \\等
String str5 = "锄禾日当午,汗滴禾下土,谁知盘中餐,粒粒皆辛苦";
String str55 = "E:\\test\\temp";
String str555 = "你|好|吗";
String[] split = str5.split(",");
String[] split2 = str55.split("\\\\");
String[] split3 = str555.split("\\|");
for (int i = 0; i < split3.length; i++) {
System.out.print(split3[i] + " ");
}
System.out.println();
//6. toCharArray 转换成字符数组
String str6 = "happy";
char[] chars = str6.toCharArray();
for (int i = 0; i < chars.length; i++) {
System.out.print(chars[i] + " ");//h a p p y
}
System.out.println();
//7. compareTo 比较两个字符串的大小,如果前者大返回正数,后者大返回负数,相等返回0
String a = "a";
String b = "c";
System.out.println(a.compareTo(b));//-2
//8. format 格式字符串
//占位符 %s:字符串,%c:字符,%d:整型,%.2f:浮点型(小数表示保留几位)
String name = "飞扬";
String name2 = "程序员飞扬";
String info = "我的姓名是%s";
String info2 = "我的公众号是%s";
String format = String.format(info, name);
String format2 = String.format(info2, name2);
System.out.println(format);//我的姓名是飞扬
System.out.println(format2);//我的公众号是程序员飞扬
}
}
三、StringBuffer类
1.基本介绍
2.String 和 StringBuffer转换
/**
* @author: 程序员飞扬
* @description:
*/
public class StringToStringBuffer01 {
public static void main(String[] args) {
//String -> StringBuffer
String str = "hello";
StringBuffer stringBuffer1 = new StringBuffer(str);//通过构造器方式
StringBuffer stringBuffer2 = new StringBuffer().append(str);//通过append方法
//StringBuffer -> String
StringBuffer stringBuffer = new StringBuffer("飞扬");
String s = stringBuffer.toString();//通过toString方法
String string = new String(stringBuffer);//通过构造器
}
}
3.StringBuffer类常用方法
/**
* @author: 程序员飞扬
* @description:
*/
public class StringBufferMethod {
public static void main(String[] args) {
//增 append
StringBuffer s = new StringBuffer("hello");
s.append(",");
s.append("张无忌").append(35).append(130.5).append(true);
System.out.println(s);
//删 delete
s.delete(15,100);//删除索引>=start&&<end的字符
System.out.println(s);
//改 replace
s.replace(6,9,"宋远桥");
System.out.println(s);
//查 indexOf
int indexOf = s.indexOf("宋远桥");//查找字符串第一次出现的索引,没有返回-1
System.out.println(indexOf);
//插入insert
StringBuffer insert = s.insert(9, "周芷若");
System.out.println(insert);
//length 获取长度
int length = s.length();
System.out.println(length);
}
}
4.练习
1)
/**
* @author: 程序员飞扬
* @description:
*/
public class StringBufferExercise01 {
public static void main(String[] args) {
String str =null;
StringBuffer sb = new StringBuffer();
sb.append(str);//底层调用的是AbstractStringBuilder的appendNull方法
System.out.println(sb.length());//4
System.out.println(sb);//null
StringBuffer sb1 = new StringBuffer(str);
System.out.println(sb1);//抛空指针异常
}
}
2)将价格转换成科学表示
/**
* @author: 程序员飞扬
* @description:
*/
public class StringBufferExercise02 {
public static void main(String[] args) {
String str = "12534254.23";
StringBuffer sb = new StringBuffer(str);
/*int i = sb.lastIndexOf(".");
StringBuffer insert = sb.insert(i - 3, ",");
System.out.println(insert);*/
for (int i = sb.lastIndexOf(".")-3; i >0; i-=3) {
StringBuffer insert = sb.insert(i , ",");
System.out.println(insert);
}
}
}
输出:
12534,254.23
12,534,254.23