正则表达式(Java小白进阶day10)

正则表达式:

♥♥小数字符串:

public static void test1(){
Scanner scanner = new Scanner(System.in);
System.out.println(“shuru”);
String str1 = scanner.next();
String string = “0\.[0-9]+”;
String string2 = "[1-9][0-9]\.[0-9]+";
if(str1.matches(string)||str1.matches(string2)) {
System.out.println(“格式正确”);
}else {
System.out.println(“格式错误”);
}
}
//错误写法[1-9]
[0-9]\.[0-9]+//不对,800009.99不可用

♥♥校验密码:

public static void test2(){
	Scanner scanner = new Scanner(System.in);
	System.out.println("shuru");
	String str1 = scanner.next();
	String string = "[a-zA-Z0-9]{8,20}";
	if(!str1.matches(string)) {
		System.out.println("密码格式错误");
		return;
	}
	int count=0;
	if (str1.matches(".*[a-z]+.*")) {
		count++;
	} if (str1.matches(".*[A-Z]+.*")) {
		count++;
	}if (str1.matches(".*[0-9]+.*")) {
		count++;
	}
	if(count >= 2) {
		System.out.println("格式正确");
	}else {
		System.out.println("格式错误");
	}
}

判断字符串:matches方法

由3个字母组成
第一给字母a/b/c
第二个字母d/e/f/g
第三个字母x/y/z
System.out.println(str.matches("[abc][defg][xyz]"));
//匹配有字母组成的字符串
System.out.println(str.matches("[a-zA-Z]"));
//匹配由数字组成的字符串
System.out.println(str.matches("[0-9]"));
//System.out.println(str.matches("\\d"));//与上行表示意思
//匹配由a开头的有两位字符构成的字符
System.out.println(str.matches("a."));
//小数点表示\\.
System.out.println(str.matches("\\."));//输出.
//表达c:\\java
//Java先转义\\,正则再转义为\
System.out.println(str.matches("c:\\\\java"));
//数量词
//+变量是之前的字符至少出现一次
System.out.println(str.matches("a.+"));//ab,aj,ajjk都行,a不可·
//*表示可有可无
System.out.println(str.matches("[a-z].*\\d"));//a1,b3,a3,fhgu4//第一位是字母,最后一个是数字,其它随便
//?至多一次
System.out.println(str.matches("a.?"));//a,a2,可,但abc不可
//匹配由5个小写字母组成的字符
//{n} 表示之前的字符恰好n次
System.out.println(str.matches("[a-z]{5}"));ababs
//匹配至少五个
//{n,}至少n个
System.out.println(str.matches("[a-z]{5,}"));
//匹配8-12个
//{n,m}字符n到m个
System.out.println(str.matches("[a-z]{8,12}"));

//删除空格(replaceAll,替换)
	String string = "  a    b   c";
System.out.println(string.replaceAll("\\s", ""));

StringBuilder

========
8中基本数据类型封装类
拆箱:Integer ->int
封箱:int ->integer
char 类型的valueOf()方法可将基本数据类型变为string型


复习:
Object
1、clone方法:克隆
用法:implements Cloneable
重写clone方法 public
浅复制:复制引用变量的地址
深复制:复制对象本身
2、toString方法:返回对象的字符表示
当打印时,默认调用
默认值:包名.类名@hashCode
3、equals方法 ==比较对象的地址
4、hashCode方法:返回Integer的数
如果equals方法返回true,hashCode相同
String类
1、final,不可以有子类
2、String str = new String(“hello”);
3、字符串不可变
final char[] value;
4、StringBuilder StringBuffer
char[] value;
♥char型直接转成字符串输出,其他类型只有转换后才可,若不转换则输出地址

猜你喜欢

转载自blog.csdn.net/hyj_123/article/details/107660047