正则表达式
验证邮箱
public class MatchesDemo {
public static void main(String[] args) {
String email = "wangcb@tedu.cn";
String regex = "[a-zA-Z0-9_]+@[a-zA-Z0-9]+(\\.[a-zA-Z]+)+";
boolean match = email.matches(regex);
if(match){
System.out.println("是邮箱");
}else {
System.out.println("不是邮箱");
}
}
}
public class SplitDemo {
public static void main(String[] args) {
String str = "abc123def456ghi789jkl";
String[] arr = str.split("[0-9]+");
System.out.println(arr.length);
for (int i = 0; i <arr.length ; i++) {
System.out.println(arr[i]);
}
str = "123.456.789";
arr = str.split("\\.");
System.out.println(arr.length);
for (int i = 0; i <arr.length ; i++) {
System.out.println(arr[i]);
}
str = ",123,,,456,789,,,,,,,,,,";
arr = str.split(",");
System.out.println(arr.length);
for (int i = 0; i <arr.length ; i++) {
System.out.println(arr[i]);
}
}
}
public class replaceAllDemo {
public static void main(String[] args) {
String str = "abc123def456ghi789jkl";
str = str.replaceAll("[0-9]+","#NUMBER#");
System.out.println(str);
String regex = "(wqnmd|sb|cnm|ngnt|nc|nmsl|mdzz)";
String message = "mdzz,你是sb吗,你全家nc";
message = message.replaceAll(regex,"***");
System.out.println(message);
}
}
public class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
@Override
public String toString() {
return "Point{" +
"x=" + x +
", y=" + y +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Point point = (Point) o;
return x == point.x &&
y == point.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
}
public class Test {
public static void main(String[] args) {
Point p = new Point(1,2);
String str= p.toString();
System.out.println(str);
System.out.println(p);
String line = "这个对象是:" + p;
System.out.println(line);
Point p2 = new Point(1,2);
System.out.println(p==p2);
System.out.println(p.equals(p2));
}
}
# 包装类
```java
/**
* 包装类
* 包装类是为了解决基本类型不能直接参与面向对象开发的问题,使得
* 基本类型可以以"对象"的形式存在。
* 包装类都定义在java.lang包中作为基础类使用。
* 六个数字类型的包装类继承自Number,其余两个直接继承自Object
*/
public class IntegerDemo {
public static void main(String[] args) {
//基本类型转化为包装类
int d = 123;
//Integer i1 = new Integer(d);
//Integer i2 = new Integer(d);
/*
基本类型转换为包装类建议使用包装类的静态方法valueOf
对于Integer而言,该方法会重用1字节之内正数的包装类对象
*/
Integer i1 = Integer.valueOf(d);
Integer i2 = Integer.valueOf(d);
System.out.println(i1==i2);//valueOf创建则为true
//包装类都重写过equals方法
System.out.println(i1.equals(i2));//true
//包装类转换为基本类型
int i = i1.intValue();//以int类型返回
System.out.println(i);
}
}
public class IntegerDemo2 {
public static void main(String[] args) {
int imax = Integer.MAX_VALUE;
System.out.println("imax:"+imax);
int imin = Integer.MIN_VALUE;
System.out.println("imin:"+imin);
String line = "123";
int in = Integer.parseInt(line);
System.out.println(in);
double dou = Double.parseDouble(line);
System.out.println(dou);
}
}
public class IntegerDemo3 {
public static void main(String[] args) {
int d = 123;
Integer i = d;
d = i;
}
}