JDK常用类

System

System是一个final类,该类的所有属性和方法都是静态的。
方法:

public static void exit(int status); //终止当前正在运行的 Java 虚拟机。参数用作状态码;根据惯例,非 0 的状态码表示异常终止

    for (int i = 0; i <1000; i++) {
        if(i==100){
            System.exit(0);//程序正常终止
        }
    }  

public static long currentTimeMillis(); //当前时间与协调世界时 1970 年 1 月 1 日午夜之间的时间差(以毫秒为单位测量)

System.currentTimeMillis(); //打印系统当前时间毫秒数     

public static void gc(); //运行垃圾回收器。暗示JVM可以执行垃圾回收了。

System.gc();   

String类

方法
1、判断字符串中下标的字符
public char charAt(int index);//返回指定索引处的 char 值

    String str="hello world";
    System.out.println(str.charAt(4));//返回“o”    

2、按字典顺序比较两个字符串
public int compareTo(String anotherString)
public int compareToIgnoreCase(String str)//忽略大小写

    String str="e";
    String str2="b";
    System.out.println(str.compareTo(str2));//返回值为3,ASCII码中e在b3位  

3、判断某个字符串是否以某个字符开头、结尾
public boolean endsWith(String suffix)//判断以某个字符结束
public boolean startsWith(String prefix)//判断以某个字符开始

    String str="hello world";
    System.out.println(str.startsWith("h"));//返回true
    System.out.println(str.endsWith("d"));//返回true  

4、截取字符串
public String substring(int beginIndex,int endIndex)//从beginIndex~endIndex-1截取字符串

    String str="hello world";
    String str2=str.substring(0, 5);
    System.out.println(str2);//结果hello    

5、字符串比较
public boolean equalsIgnoreCase(String anotherString)//忽略大小写比较。

6、将字符串转换为byte数组
public byte[] getBytes()//将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中

    String str="hello world";
    byte []bt=str.getBytes();
    System.out.println(Arrays.toString(bt));//将字符串转换为byte数组,打印为ascii码  

6、查找某个字符在字符串中的下标
public int indexOf(String str)

    String str="hello world";
    System.out.println(str.indexOf("e"));//判断字符e在字符串中的下标为1    

7、字符串替换
public String replace(char oldChar,char newChar)

    String str="hello world";
    String str1=str.replace("llo", "good");
    System.out.println(str1);//结果为“hegood world”  

8、按规则拆分字符串
public String[] split(String regex)

    String str="hello@world";
    String []str1=str.split("@");
    System.out.println(Arrays.toString(str1));//以“@”为规则,拆分字符串str  

9、大小写转换
public String toLowerCase(Locale locale)//转换小写
public String toUpperCase(Locale locale)//转换大写

10、截取字符串
public String substring(int beginIndex,int endIndex)//按下标截取字符串

    String str="hello@world";
    String str1=str.substring(0,6);
    System.out.println(str1);//结果为hello@  

11、去掉字符串前、后的空白
public String trim()

    String str="    hello@world    ";
    String str1=str.trim();
    System.out.println(str1);//结果为hello@world

Calendar类

方法:
public void add(int field, int amount)//将制定的数字添加到日历字段中
public int get(int field)//返回给定日历字段的值。
public static Calendar getInstance()//使用默认时区和语言获得一个日历

    Calendar calendar=new GregorianCalendar();
    System.out.println(calendar.get(Calendar.YEAR));
    System.out.println(calendar.get(Calendar.MONTH)+1);
    System.out.println(calendar.get(Calendar.DATE));
    calendar.add(Calendar.DATE, 8);
    System.out.println(calendar.get(Calendar.YEAR)+","+calendar.get(Calendar.MONTH)+1+","+calendar.get(Calendar.DATE));  
    System.out.println(calendar.getInstance());//返回Calendar的对象实例  

Math类

1、取绝对值
public static double abs(double a)

    int a=-10;
    System.out.println(Math.abs(a));    

2、向上取整
public static double ceil(double a)

    double a=2.1;
    System.out.println(Math.ceil(a));//值为3  

3、向下取整
public static double floor(double a)

    double a=2.8;
    System.out.println(Math.floor(a));///值为2    

4、四舍五入
public static long round(double a)

    double a=2.8;
    System.out.println(Math.round(a));///值为3   

5、随机数
public static double random()
Math.random();

自动装箱及拆箱

自动装箱

    int num=1;
    Integer i=new Integer(num);//自动装箱  

自动拆箱

    Integer i=new Integer(10);
    int num=i;//自动拆箱  

字符串转换为数字

    String str="1000";
    int num=Integer.parseInt(str);//字符串转换为数字
    System.out.println(num);   

正则表达式

规则
/…/ 代表一个模式的开始和结束 ^ 匹配字符串的开始 $ 匹配字符串的结束 \s 任何空白字符 \S 任何非空白字符 \d 匹配一个数字字符,等价于[0-9] \D 除了数字之外的任何字符,等价于[^0-9] \w 匹配一个数字、下划线或字母字符,等价于[A-Za-z0-9] \W 任何非单字字符,等价于[^a-zA-z0-9] . 除了换行符之外的任意字符

{n} 匹配前一项n次
{n,}    匹配前一项n次,或者多次
{n,m}   匹配前一项至少n次,但是不能超过m次
*   匹配前一项0次或多次,等价于{0,}
+   匹配前一项1次或多次,等价于{1,}
?   匹配前一项0次或1次,也就是说前一项是可选的,等价于{0,1}

x|y
匹配x或y。例如,“z|food”能匹配“z”或“food”。“(z|f)ood”则匹配“zood”或“food”。
[xyz]
字符集合。匹配所包含的任意一个字符。例如,“[abc]”可以匹配“plain”中的“a”。
[a-z]
字符范围。匹配指定范围内的任意字符。例如,“[a-z]”可以匹配“a”到“z”范围内的任意小写字母字符。
注意:只有连字符在字符组内部时,并且出现在两个字符之间时,才能表示字符的范围; 如果出字符组的开头,则只能表示连字符本身.
(pattern)
匹配pattern并获取这一匹配。所获取的匹配可以从产生的Matches集合得到,在VBScript中使用SubMatches集合,在JScript中则使用$0…$9属性。要匹配圆括号字符,请使用“\(”或“\)”。  

//邮箱规则

    Pattern p=Pattern.compile("^\\w{1,14}@((163)|(126)|(qq)|(gmail)).((com)|(cn))$");
    Matcher m=p.matcher("jinyouqiang000@gmail.com");
    System.out.println(m.matches());    

//电话号码规则

    Pattern p1=Pattern.compile("^((139)|(159)|(134))\\d{8}$");
    Matcher m1=p1.matcher("13911111111");
    System.out.println(m1.matches());  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值