Java中的常用类(API)

1.正则表达式

正则表达式(regex)是一个字符串,使用一些特殊的符号来制定一个规则,然后用这个规则与字符串匹配,是一种模式匹配语法。

matches 函数中的正则表达式:

"\d" 匹配数字 等同于[0-9]

"\w" 匹配字母和数字和下划线

"\s" 匹配空白字符

"[3-9]" 数字从3到9

"[357]" 只允许3,5,7出现

"[^357]" 不允许3,5,7出现

"[A-z]" 所有的大小写字母

* 允许出现的次数是0次或多次

+ 一次或多次出现

?0次或者一次

{n} 恰好出现n次

{n,} 至少出现n次

{n,m} 至少n次,至多m次

"." 匹配除换行符外的任意字符

2.StringBuffer类和String类

String类中的函数

● 替换功能
String replace(char old,char new)
String replace(String old,String new)
replaceAll(String regex, String replacement)
replaceFirst(String regex, String replacement)
● 去除字符串两空格
String trim()     //去除字符串收尾的空格

StringBuffer类

● StringBuffer类概述
我们如果对字符串进行拼接操作,每次拼接,都会构建一个新的String
对象,既耗时,又浪费空间。而StringBuffer就可以解决这个问题
线程安全的可变字符序列

● 构造方法
public StringBuffer()
public StringBuffer(String str)

● 添加功能
public StringBuffer append(String str)
public StringBuffer insert(int offset,String str)
● 删除功能
public StringBuffer deleteCharAt(int index)
public StringBuffer delete(int start,int end)
● 替换功能
public StringBuffer replace(int start,int end,String str)
● 反转功能
public StringBuffer reverse()

● 截取功能
public String substring(int start)
public String substring(int start,int end)
● 截取功能和前面几个功能的不同
返回值类型是String类型,本身没有发生改变
● StringBuilder类功能和StringBuffer功能完全一致, StringBuffer是线程
安全的

public class StringBufferdemo1 {
    public static void main(String[] args) {
        //StringBuffer b=new StringBuffer("ABC");
        //StringBuffer b=new StringBuffer("");
        StringBuffer a=new StringBuffer(10);
        a.append("aa");
        a.append("bbbbbbbbbbb");//数组扩容 原来长度乘以2再加2 ,不会创建新的Stringbuffer对象
        a.delete(1,4);
        a.deleteCharAt(1);
        String ss=a.substring(0);//返回值类型是String类型,本身没有发生改变
        System.out.println(ss);
        a.replace(0,2,"11");
        System.out.println(a);
        System.out.println(a.reverse());
        StringBuilder c=new StringBuilder("aaa");
        c.append(1);
        System.out.println(c.reverse());
        System.out.println(c);
    }

String类StringBuffer类StringBuilder区别

● String:是字符常量,适用于少量的字符串操作的情况
● StringBuilder:适用于单线程下在字符缓冲区进行大量操作的情况
● StringBuffer:适用多线程下在字符缓冲区进行大量操作的情况

3.Math类

java.lang.Math提供了一系列静态方法用于科学计算;其方法的参数和
返回值类型一般为double型。
abs 绝对值
sqrt 平方根
pow(double a, double b) a的b次幂
max(double a, double b)
min(double a, double b)
random() 返回 0.0 到 1.0 的随机数
long round(double a) double型的数据a转换为long型(四舍五入)

public class MathDemo {
    public static void main(String[] args) {
        System.out.println(Math.PI);
        System.out.println(Math.abs(-9));
        System.out.println(Math.sqrt(2.0));
        System.out.println(Math.ceil(2.1));
        System.out.println(Math.floor(2.3));
        System.out.println(Math.pow(2,3));
        System.out.println(Math.round(2.2));
        System.out.println(Math.round(2.7));
        System.out.println(Math.random());
    }
}

4.Random类

● Random类概述
此类用于产生随机数
● 构造方法
public Random()
l 成员方法
public int nextInt()
public int nextInt(int n)

public class RandomDemo {
    public static void main(String[] args) {
        Random random=new Random();
        System.out.println(random.nextInt());
        System.out.println(random.nextDouble());
        System.out.println(random.nextInt(10));
        byte[] bytes=new byte[4];
        random.nextBytes(bytes);
        System.out.println(Arrays.toString(bytes));

    }
}

5.Date类/Calendar类/ SimpleDateFormat类

 Date类

● 使用Date类代表当前系统时间
Date d = new Date();
Date d = new Date(long d);

public class DateDemo {
    public static void main(String[] args) {
        Date date=new Date();
        System.out.println(date);
        //java中的过期方法,不建议使用
        System.out.println(date.getDate());
        System.out.println(date.getMonth());
        System.out.println(date.getYear());
        Long e=date.getTime();
        for(int i=0;i<100000;i++){

        }
        Date date1=new Date();
        Long s=date1.getTime();
        System.out.println(s-e);
    }

}

Calendar类

● Calendar类是一个抽象类,在实际使用时实现特定的子类的对象,创建
对象的过程对程序员来说是透明的,只需要使用getInstance方法创建
即可。
Calendar c1 = Calendar.getInstance();
c1.get(Calendar. YEAR);

public class Calendedemo {
    public static void main(String[] args) {
        Calendar calendar=new GregorianCalendar();
        Calendar calendar1=Calendar.getInstance();
        System.out.println(calendar.getTime());
        System.out.println(calendar.getTimeInMillis());
        System.out.println(calendar.get(Calendar.YEAR));
        System.out.println(calendar.get(Calendar.MONTH));

    }


}

SimpleDateFormat 日期格式化类

● 构造方法
SimpleDateFormat(格式); // yyyy-MM-dd
● 日期转字符串
Date now=new Date();
myFmt.format(now);
● 字符串转日期
myFmt.parse(“2018-02-10”);
字符串日期格式与 指定格式必须一致
例如:String s = “2018-03-15”;
new SimpleDateFormat(“yyyy-MM-dd”)

public class SimpleDateFormatdemo {
   /* public static void main(String[] args) {
        //日期转字符串
        Date date=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("YYYY年MM月dd日 HH:mm:ss E");
        String s=simpleDateFormat.format(date);
        System.out.println(s);
    }
*/
    //字符串转日期对象
   public static void main(String[] args) throws ParseException {
       String s="2002-07-01";
       SimpleDateFormat simpleDateFormat=new SimpleDateFormat("YYYY-MM-dd");
       Date date=simpleDateFormat.parse(s);
       System.out.println(date.getMonth());
   }
}

猜你喜欢

转载自blog.csdn.net/weixin_64189867/article/details/130893461