回顾:
Object:toString equals hashCode
String:字符串 - 不可变的
final char[]
int length()
char charAt(int)
常用API
正则表达式
boolean matches(String regex)
String replaceAll(String regex, String newStr)
String[] split(String regex)
只能出现 0-9 a-z A-Z _ $, 一共最多16个字符,最少6个字符
输入正确身份证号
11位电话号码
IP地址 xx.xxx.xxx.xxx
xxx@xx.com.cn
La-z(2~4) -> L(\dcom){2,4}
数量词
X? X,一次或一次也没有
X* X,零次或多次
X+ X,一次或多次
X{n} X,恰好 n 次
X{n,} X,至少 n 次
X{n,m} X,至少 n 次,但是不超过 m 次
预定义字符类
. 任何字符
\d 数字:[0-9]
\s 空白字符:[ \t\n\x0B\f\r]
\w 单词字符:[a-zA-Z_0-9]
一个163邮箱的地址
[a-zA-Z]\w{5,17}@163\.com
字符串:由字符数组,Unicode编码集
1个字符=2个字节
操作系统中,文件存储都是字节单位
可变字符串
StringBuilder:线程不安全,效率高
StringBuffer :synchronized - 同步锁/并发 线程
线程安全,效率低
频繁改变字符串,就使用StringBuilder
java.lang -> 默认包
java.util -> 工具
long - 时间 毫秒
Date - 时间 年月日、时分秒
java.text: 格式化的
时间格式化 DateFormat SimpleDateFormat
String format(Date date) – 时间格式化
Date -> 2019-05-12 19:53:28
Date parse(String source) – 时间字符串的解析
2019-05-12 19:53:28 -> Date
作业:
~ day03/Test02 -> 码云
优快云:2篇笔记
Date:
@Test
public void test03() {//时间格式化! format时间转换成字符串
Date d1 = new Date();
System.out.println(d1);
DateFormat df =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//自定义格式
String str = df.format(d1);
System.out.println(str);
}
@Test
public void test04() throws ParseException {//parse将字符串转换成时间
DateFormat df =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//指定时间 2020-5-12 12:30:29
Date d = df.parse("2020-5-12 12:30:29");
System.out.println(d);
}
StringBuilder:
@Test
public void test02() {
//String 不可变的
//StringBuilder 可变的
StringBuilder sb = new StringBuilder("hello");
System.out.println(sb);
StringBuilder sb1 = sb.append("hi");
System.out.println(sb);
System.out.println(sb == sb1);
// append() - 向字符串末尾追加
// insert() - 在指定位置上插入
// delete() - 删除指定位置
// replace() - 替换指定位置元素
}
StringTest:
@Test
public void test01() {//字符串拼接
String s1 = "hello";
s1 = s1.concat("hi");
System.out.println(s1);
}
@Test
public void test02() {//字符串截取
String email = "szl123@163.com";
String user = email.substring(3);
System.out.println(user);
user = email.substring(0, 7);//截取前包含后不包含
System.out.println(user);
int index = email.indexOf("@");//
user = email.substring(0, index);
System.out.println(user);
}
@Test
public void test03() {//字符串替换。
String str = "qnmlgb,你真是个大sb";
str = str.replace("qnmlgb", "******");
System.out.println(str);
}
@Test
public void test04() {//判断是否为空
String str = "";
System.out.println(str.isEmpty());
}
@Test
public void test05() {
String str = "shlzihelhzin";
System.out.println(str.lastIndexOf("l"));//返回指定字符在此字符串中最后一次出现处的索引
System.out.println(str.lastIndexOf("hz"));//返回指定字符在此字符串中最后一次出现处的索引
System.out.println(str.lastIndexOf("z",5));//返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。
}
@Test
public void test06() {
String str = "ShiZheLin";
System.out.println(str.toUpperCase());//转换成大写输出
System.out.println(str.toLowerCase());//转换成小写输出
System.out.println(str.toCharArray());//转换成字符串数组
System.out.println();
}
@Test
public void test07() {
String str = "janejarryasd";
System.out.println(str.substring(4));//返回一个新的字符串,从4下标开始
System.out.println(str.substring(4,9));// 前包含后不包含,返回新字符串
System.out.println(str.subSequence(4,9));//前包含后不包含,返回新序列
}
@Test
public void test08() {
String str = "www.shzihelin.com";
System.out.println(str.startsWith("www."));//是否以指定字符开始
}
@Test
public void test09() {//正则表达式 ismatches
String email = "Shizhelin123@163.com";
String regex = "[a-zA-Z]\\w{5,17}@163\\.com";
System.out.println(regex);
boolean ismatches = email.matches(regex);
System.out.println(ismatches);
}
@Test
public void test10() {//用replaceAll 正则表达式 隐藏脏话。
String str = "qnsbqnmlgb,qn你真是个大sb,zz";
String regex = "(((qn)?mlgb)|sb|zz|nc)+";
str = str.replaceAll(regex, "**");
System.out.println(str);
}
@Test
public void test11() {//用正则表达式将 192 168 6 66放入字符串数组中。
String str = "192.168.6.66";
String regex = "\\.";
String ss[] = str.split(regex);
System.out.println(Arrays.toString(ss));
}
@Test
public void test12() {//将英文单词放入字符串数组中,并计算长度
String str = "hello hi,what's your name.my name is jerry!and you?";
//,.!?空格
//[,\.! \?]+加号不在 感叹号或者……加空格 中间有一个空字符串。所以要加加号!
String regex = " |\\,|\\.|\\!|\\?";
String arr[] = str.split(regex);
System.out.println(Arrays.toString(arr));
System.out.println(arr.length);
}
包装类:
@Test
public void test01() {
long time1 = System.currentTimeMillis();
System.out.println(time1);
//1557651077250 2019年5月12号 16:53
//0 =>计算机元年 1970年1月1日 00:00:00 这个时间就是开始计算的时间,格林威治时间。
/*
* byte:-128 ~ 127
* short: -2^15 ~ x^15-1 =>32200
* int: -2^31 ~ 2^31-1 =>2147480000
* long: -2^63 ~ 2^63 -1 =>??
*
*/
//byte\short\int\long\float\double\boolean\char
//Object
//为了符合一切皆对象,JDK给8个基本数据类型都提供了对应的包装类
//Byte\Short\Integer\Long\Float\Double\Boolean\Character
}
@Test
public void test02() {
int a = 1;
Integer a1 = 1;
//将Integer类型赋值给int类型 -> 自动拆箱 -> 编译器自动装箱
a = a1.intValue();//相当于 a = a1
//将int类型赋值给Integer类型 -> 自动装箱
a1 = a;//a1 = Integer.ValueOf(a);
System.out.println(a == a1);
}
@Test
public void test03() {//字符串变成整数。
String str = “1”;
int i = Integer.valueOf(str);
i=i+2;
System.out.println(i);//输出 3
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
System.out.println(Long.MAX_VALUE);
System.out.println(Long.MIN_VALUE);
}