一。正则表达式:字符串的一些排列规则
格式的校验: 邮箱格式/身份证格式/电话号码格式
长短的判断: 至少多少个字符, 最长多少个字符
内容的限定: 只能有字母\数字\下划线
开始符号: ^
结束符号: $
转义: 有特殊意义的字符
. \ + ? * -
表示字符本身, 需要 \字符
. \ + ? * -
1.matches
public class Stringmatchs {
public static void main(String[] args) {
String m1="ooodddll@qqq.com.edu.cn";
String m="^[\\w]{8}@[\\w]{2,3}(\\.[\\w]{2,3}){1,6}";
boolean q=m1.matches(m);//m1和m的正则表达式比较
System.out.println(q);
}
}
2.replaceAll
public class StringreplaceAll {
public static void main(String[] args) {
String str="李白nc,就是sb";
String str1="[nc]|[sb]";
//将str1中的字符替换
String str3= str.replaceAll(str1,"*");
System.out.println(str3);
}
}
3.split
public class Stringsplist {
public static void main(String[] args) {
String str="ZZ ==was 8*/|\\ years olds ";
String str1="[\\W]+";
//按照不是英文字母拆分
String[] arry=str.split(str1);
System.out.println(Arrays.toString(arry));
}
}
二。String:
String 是不可变的, 所以频繁的字符串拼接非常消耗时间/内存
解决方案: 提供了新的类, 可变字符串 StringBuffer StringBuilder
StringBuffer : 线程安全的
StringBuilder: 效率高
构造方法:
StringBuilder sb = new StringBuilder() -> “”
String str = sb.toString()
StringBuilder sb = new StringBuilder(String)
常用API: - 返回值都是this, 在对象本身进行修改
append(Object o) -> 在字符串末尾追加新的内容
delete(int start, int end)
replace(int start, int end, String replacement)
insert(int index, String str)
reverse()
public class StringBuild1 {
public static void main(String[] args) {
StringBuilder str=new StringBuilder();
//字符串拼接-末尾追加
str.append("haha");
str.append("heihei");
//在指定位置插入字符串
str.insert(4,"xixi");
//将start和end中间的字符替换成指定字符串
str.replace(0,4,"hehe");
//将StringBuild转换成String
String str1=str.toString();
//回文
str.reverse();
}
}
三。包装类: 将基本数据类型做了封装,
每一种基本数据类型都对应一种包装类型
一切皆对象 - 引用, 基本数据类型不属于对象
byte -> Byte
short -> Short
int -> Integer
long -> Long
double -> Double
float -> Float
char -> Character
boolean -> Boolean
所有的数值的包装类都拥有统一的父类: Number
所有的包装类, 都是 final 类
Integer: 常量池, 范围 -128 ~ 127
public class ISB {
public static void main(String[] args) {
int i=1;
Integer i1=2;//包装类对象必须初始化赋值
//包装类可以new对象
i1=null;//i1为引用类对象,可以为null;
i1=new Integer(5);
System.out.println(i1+i);
//基本类型转包装类型
Integer i2=Integer.valueOf(i);
//将字符串转换为对应进制的基本类型("字符串",进制)
int m=Integer.parseInt("ff",16);
System.out.println(m);
//包装类型转基本类型
int n=i1.intValue();
System.out.println(n);
//基本数据类型对象和包装类对象的相互赋值
i1=i;//等同于i1=Integer.valueOf(i);
i=i1;//等同i=i1.intValue();
//Integer也有常量池-128~127
i1=125;
i2=125;
System.out.println(i1==i2);
}
}
四。集合:
java.util.Collection Map -> 接口
Collection coll = new ArrayList()
Collection的方法只有 添加\移除\清空\判断是否为空\判断是否包含\长度
注意: 没有单独获得某个元素的方法, 只能迭代/遍历
Iterable -> 可迭代的, iterator() -> 获得迭代器
迭代器: 获得迭代器时, 游标指向集合中第一个元素之前
public class Collection1 {
public static void main(String[] args) {
//Collection是抽象类,要通过实现类来new对象
Collection<String> con=new ArrayList<>();
con.add("oppo");//添加元素
con.add("vivo");
con.add("iphon");
con.add("banana");
con.add("appal");
con.remove("vivo");//删除指定元素,只删除一个
boolean u=con.contains("vivo");//判断数组中是否包含vivo,返回boolean
con.isEmpty();//判断是否为空,返回boolean
Iterator<String> it=con.iterator();//迭代
while (it.hasNext()){//判断指针下次内容是否为空
String str=it.next();
System.out.println(str);
}
Collection<String> con1=new ArrayList<>();
con1.add("吉他");
con1.add("钢琴");
con1.add("钢琴");
con.addAll(con1);//将con1数组所有内容加到con中
System.out.println(con);
Collection<String> con2=new ArrayList<>();
con2.add("吉他");
//con2.add("钢琴");
//con1中是否包含全部con2
con1.containsAll(con2);
//con中只保存con1中出现的元素
con.retainAll(con1);
//将con2中的元素在con中删除
con.removeAll(con2);
System.out.println(con);
}
}