day14--java作业

A:简答题
1、所有类中的方法在API中找到,并自己加上描述:
String:
public boolean matches(String regex)
public String[] split(String regex)
public String replaceAll(String regex,String replacement)
Math:
public static int abs(int a)
public static double ceil(double a)
public static double floor(double a)
public static int max(int a,int b)
public static double pow(double a,double b)
public static double random()
public static int round(float a)
public static double sqrt(double a)
Random:
public int nextInt()
public int nextInt(int n)
System
public static void gc()
public static void exit(int status)
public static long currentTimeMillis()
BigDecimal
public BigDecimal add(BigDecimal augend)
public BigDecimal subtract(BigDecimal subtrahend)
public BigDecimal multiply(BigDecimal multiplicand)
public BigDecimal divide(BigDecimal divisor)
public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode)
Date
public long getTime()
public void setTime(long time)
SimpleDateFormat
public final String format(Date date)
public Date parse(String source)
Calendar
public static Calendar getInstance()
public int get(int field)
public void add(int field,int amount)
public final void set(int year,int month,int date)
2、什么是正则表达式?

正则表达式(Regular Expression)是一种文本模式,在编写处理字符串的程序或网页时,经常会有查找符合某些规则的字符串的需求。正则表达式就是用于描述这些规则的工具,换句话说,正则表达式就是记录文本规则的代码。
一、正则表达式常用字符

1) 常用元字符

. 匹配除换行符以外的任意字符。
\w 匹配字母、数字、下划线。
\s 匹配任意的空白符。
\d 匹配数字。
\b 匹配单词的开始或结束。匹配符合表达式规则,并且以英文单词的形式出现(前后有空格)。如:var reg = /abc\b/; 匹配”abc 123 abc abcdey abc”,结果为:abc abc。
^ 匹配字符串的开始(从字符串的第一个字符开始匹配),如果不指定开始和结束,将匹配字符串中任意位置的字符。如:var reg = /[1-9]{1,}/; 匹配”abc12345dey”,结果为:12345。
$ 匹配字符串的结束(匹配到字符串的最后一个字符)。

2) 常用限定符

* 重复零次或多次。
+ 重复一次或多次。
? 重复零次或一次。
{n} 重复 n次。
{n,} 重复 n次或多次。
{n,m} 重复 n次到m次。

3) 常用反义词

\W 匹配任意不是字母,数字,下划线,汉字的字符。
\S 匹配任意不是空白的字符。
\D 匹配任意非数字的字符。
\B 匹配不是单词开头或结束的位置。
[^x] 匹配除了x以外的任意字符。
[^aeiou]匹配除了aeiou这几个字母以外的任意字符。

特殊字符”\”
1) 该字符可以将元字符转义为常量,例如:”\.”,将元字符”.”,转为义为常量”.”。

2) 该字符还可以将常量转义为元字符,例如:”\w”,将常量”w”,转为义为元字符”\w”。

二、正则表达式修饰符

1) /g 表示该表达式将用来在输入字符串中查找所有可能的匹配,返回的结果可以是多个,如果不加/g最多只会匹配一个。

2) /i 表示匹配字符串时不区分大小写。

3) /m 表示多行匹配。什么是多行匹配呢?就是匹配换行符两端的潜在匹配,映象正则中^$符号。

三、示例

1) 使用 [] 限定范围

[abc] 字符串中某个字符出现表达式中,则匹配成功。例如匹配”1a2b3c”,结果为:a b c。
[a-z1-9]] 字符串中某个字符出现表达式的范围中,则匹配成功。例如匹配”ahzAZ0139@”,结果为:a h z 1 3 9。
[a-zA-Z0-9] 字符串中某个字符出现表达式的范围中,则匹配成功。例如匹配”ahzAZ0139@”,结果为:a h z A Z 0 1 3 9。

2) 使用 (|) 限定组
Window(95|98|NT|2000) 某个字符串匹配正则表达式中的多个分组,例如匹配”Window95WindowWindow98window98WindowNT”,结果为:Window95 Window98 WindowNT。

3) 匹配中文
[^x00-xff]{1,} 匹配中文字符串,例如匹配”啊1a看.~!@#$%^&*(),./\][{}-+,结果为:啊 看.~! #$% &*(),./ {}-+。匹配不准确!
[\u4e00-\u9fa5]{1,} 匹配中文字符串,例如匹配”啊1a看.~!@#$%^&*(),./\][{}-+,结果为:啊 看。匹配准确!

3、如何实现Date与long相互转换?
4、如何实现Date与String相互转换?


// date类型转换为String类型
    // formatType格式为yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
    // data Date类型的时间
    public static String dateToString(Date data, String formatType) {
        return new SimpleDateFormat(formatType).format(data);
    }

    // long类型转换为String类型
    // currentTime要转换的long类型的时间
    // formatType要转换的string类型的时间格式
    public static String longToString(long currentTime, String formatType)
            throws ParseException {
        Date date = longToDate(currentTime, formatType); // long类型转成Date类型
        String strTime = dateToString(date, formatType); // date类型转成String
        return strTime;
    }

    // string类型转换为date类型
    // strTime要转换的string类型的时间,formatType要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
    // HH时mm分ss秒,
    // strTime的时间格式必须要与formatType的时间格式相同
    public static Date stringToDate(String strTime, String formatType)
            throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat(formatType);
        Date date = null;
        date = formatter.parse(strTime);
        return date;
    }

    // long转换为Date类型
    // currentTime要转换的long类型的时间
    // formatType要转换的时间格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
    public static Date longToDate(long currentTime, String formatType)
            throws ParseException {
        Date dateOld = new Date(currentTime); // 根据long类型的毫秒数生命一个date类型的时间
        String sDateTime = dateToString(dateOld, formatType); // 把date类型的时间转换为string
        Date date = stringToDate(sDateTime, formatType); // 把String类型转换为Date类型
        return date;
    }

    // string类型转换为long类型
    // strTime要转换的String类型的时间
    // formatType时间格式
    // strTime的时间格式和formatType的时间格式必须相同
    public static long stringToLong(String strTime, String formatType)
            throws ParseException {
        Date date = stringToDate(strTime, formatType); // String类型转成date类型
        if (date == null) {
            return 0;
        } else {
            long currentTime = dateToLong(date); // date类型转成long类型
            return currentTime;
        }
    }

    // date类型转换为long类型
    // date要转换的date类型的时间
    public static long dateToLong(Date date) {
        return date.getTime();
    }

B:编程题
1、请编写程序,校验键盘录入的电子邮箱是否合法,并测试
//这是找到的校验邮箱正则表达式
String regex = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";

//校验邮箱的规则
//2677759629@qq.com
//zhangsan@163.com
String mailRgex="[a-zA-Z0-9]{6,16}@[a-z0-9]{2,16}\\.(com|cn|net|org)";
boolean c = "shenpeiqing168@163.cc".matches(mailRgex);
System.out.println(c);

//校验手机号
String regx="[1][35678][0-9][0-9]{8}";
boolean b = "13259141515".matches(regx);
System.out.println(b);

//校验身份证的正则
String string="[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]";

2、请编写程序,把给定字符串中的数字排序
给定的字符串是: “91 27 -45 46 38 50”
最终输出结果是: “-45 27 38 46 50 91”

//需求:如下一个字符串:”91 27 46 38 50”,请写代码实现最终输出结果是:”27 38 46 50 91”
String string="91 27 46 38 50";  //转成此结果  "27 38 46 50 91"
//思路:1.截取空格 返回一个字符串数组
//2.将一个字符串数组,转成一个int类型的数组
//3.排序
//5.遍历数组,拼串
String[] strs = string.split(" ");
int[] arr=new int[strs.length];
//遍历字符串数组的元素,装到int数组里面去
for(int i=0;i<strs.length;i++) {
    arr[i]=Integer.parseInt(strs[i]);
}
//排序
Arrays.sort(arr);
//System.out.println(Arrays.toString(arr));
//遍历int数组 拼串
//String string2="";
StringBuffer sb = new StringBuffer();
for(int i=0;i<arr.length;i++) {
    sb.append(arr[i]).append(" ");
}
String string2 = sb.toString().trim();
System.out.println(string2);
String string="奥巴马和普京是好基友";
String replaceAll = string.replace("奥巴马","*").replaceAll("普京","*");
System.out.println(replaceAll);

//根据正则表达式去替换
String string2="asdfasfd1222你好asdfasdf9999asdfAAAa中国";
String replace = string2.replaceAll("\\w+","");
System.out.println(replace);


String string3="abc|acc|ddd";
//对一些特殊字符需要转意
String[] split = string3.split("\\|");
System.out.println(Arrays.toString(split));

3、请编写程序,完成获取指定的日期 与今天相距多少天,并测试

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dstr="2008-08-08 08:08:08 ";
java.util.Date date=sdf.parse(dstr);
long s1=date.getTime();//将时间转为毫秒
long s2=System.currentTimeMillis();//得到当前的毫秒
int day=(s2-s1)/1000/60/60/24;
System.out.println("距现在已有"+day+"天,你得抓紧时间学习了" );

4、请编写程序,完成文件路径的切割,并测试

filePath=‪"C:\\Users\\think\\Pictures\\archive\\timg1.jpg";
String[] fileName=file.split("\\\\");
for(String name:fileName){
System.out.println(name);
}
}
//去字符串数组的最后一个元素
String filePath=‪"C:\\Users\\think\\Pictures\\archive\\timg1.jpg";
String[] aa=filePath.split("\\\\");
String name=aa[aa.length-1];
System.out.println(name);

5、请编写程序,校验键盘录入的用户名是否合法,并测试
要求:用户名必须是6-16位之间的字母下划线或者数字

 String regex = "[a-zA-Z0-9_]{6,16}";
        ret = string.matches(regex);
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

phial03

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值