-----------android培训、java培训、java学习型技术博客、期待与您交流!------------
在此,分享一下自己学习JAVA的学习心得。有不对的地方请帮忙改正,也希望对想学java的同学有帮助!
JAVA基础
——API—常用类(三)
5)正则表达式(Pattern,Matcher):
正则表达式:
是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串。其实就是一种规则。有自己特殊的应用。
正则表达式也是用来进行文本匹配的工具,只不过比起通配符,它能更精确地描述你的需求。
正则表达式的组成规则:
1)规则字符在java.util.regex Pattern类中
2)常见组成规则
(1)字符
(2)字符类
(3)预定义字符类
(4)边界匹配器
(5)数量词
正则表达式——字符类:
正则表达式的字符类:
Java练习代码:
public class Demo {
public static void main(String[] args) {
String str = "hid";
//字符串中是否以h开头,以d结尾,而且中间只有一个字符,而且是元音字母a、e、i、o、u?
String regex = "h[aeiou]d";
System.out.println(str.matches(regex));
//字符串中是否以h开头,以d结尾,而且中间只有一个小写英文字母?
regex = "h[a-z]d";
System.out.println(str.matches(regex));
//字符串是否以大写英文字母开头,后跟id?
regex = "[A-Z]id";
System.out.println(str.matches(regex));
//字符串首字母是否非数字?
regex = "[^0-9]id";
System.out.println(str.matches(regex));;
//练习:以b 或 c 或 d 开头,中间字符是 a,并且以 d 或 t 结尾 的字符串;
regex = "[bcd]a[dt]";
System.out.println("1." + ("bad".matches(regex)));//true
System.out.println("2." + ("cbd".matches(regex)));//false
System.out.println("3." + ("cat".matches(regex)));//true
}
}
正则表达式——逻辑运算符:
正则表达式中的逻辑运算符
Java练习代码:
public class Demo {
public static void main(String[] args) {
//1.判断小写辅音字母:
String str = "Z";
String regex = "[a-z&&[^aeiou]]";
System.out.println(str.matches(regex));
//2.判断首字母为h 或 H ,后跟一个元音字母,并以 d 结尾:
String str2 = "Had";
regex = "[hH][aeiou]d";
regex = "[h|H][a|e|i|o|u]d";
System.out.println(str2.matches(regex));
}
}
正则表达式——预定义字符类:
正则表达式中的预定义字符类:
注:在Java中将反斜线看做转义序列的开头。因此必须在正则表达式中将反斜线指定为:\\:
判断是否是三位数字:“\\d\\d\\d”;等同于:“[0-9][0-9][0-9]”
判断字符串是否以h开头,中间是任何字符,并以d结尾:“h.d”
判断字符串是否是”had.”:“had\\.”
判断手机号码(1开头,第二位是:3或5或8,后跟9位数字):“1[358]\\d\\d\\d\\d\\d\\d\\d\\d\\d”
Java代码:
public class Demo {
public static void main(String[] args) {
//1.判断是否是三位数字:
String str = "234";
String regex = "[0-9][0-9][0-9]";
regex = "\\d\\d\\d";//效果同上一行;
System.out.println(str.matches(regex));
//2.判断字符串是否以h开头,中间是任何字符,并以d结尾:“h.d”
str = "had.";
regex = "h.d";
System.out.println(str.matches(regex));
//3.判断字符串是否是"had."
regex = "had\\.";
System.out.println(str.matches(regex));
//4.判断手机号码(1开头,第二位是:3或5或8,后跟9位数字):
str = "15838386789";
regex = "1[358]\\d\\d\\d\\d\\d\\d\\d\\d\\d";
System.out.println(str.matches(regex));
System.out.println();
}
}<strong>
</strong>
正则表达式——限定符:
限定符位于模式中子序列的后侧,确定了子序列的重复方式。
注:判断1位或多位数字:“\\d+”;
判断手机号码:“1[358]\\d{9}”;
判断小数(小数点最多1次):“\\d+\\.?\\d+”
判断数字(可出现或不出现小数部分):“\\d+(\\.\\d+)?”
Java代码:
public class Demo {
public static void main(String[] args) {
//1.判断1位或多位数字:
String str = "123474324";
String regex = "\\d+";
System.out.println(str.matches(regex));
//2.判断手机号码:
str = "18516955778";
regex = "1[358]\\d{9}";
System.out.println(str.matches(regex));
//3.判断小数(小数点最多1次):
str = "3.14";
regex = "\\d+\\.?\\d+";
System.out.println(str.matches(regex));
//4.判断数字(可出现或不出现小数部分):
regex = "\\d+(\\.\\d+)?";
System.out.println(str.matches(regex));
//5.判断一个QQ号码(5-15位数字,不能以0开头):
str = "587302";
regex = "[1-9]\\d{4,14}";
System.out.println(str.matches(regex));
//6.参考并修改上面第四个例子,使之满足:“22.”的格式;
str = "3.143";
regex = "\\d+(\\.\\d*)?";
System.out.println(str.matches(regex));
//7.满足:+20,-3,22.格式:
str = "-20.1432432";
regex = "[+-]?\\d+(\\.\\d*)?";
System.out.println(str.matches(regex));
}
}
正则表达式——分组:
验证如下序列号:
String str = “DG8FV-B9TKY-FRT9J-6CRCC-XPQ4G”;
方式一:
str.matches(“[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}”;
方式二:
str.matches("([A-Z0-9]{5}-){4}[A-Z0-9]{5}");
或
str.matches(“[A-Z0-9]{5}(-[A-Z0-9]{5}){4}”);
Java代码:
public class Demo {
public static void main(String[] args) {
String str = "DG8FV-B9TKY-FRT9J-6CRCC-XPQ4G";
String regex = "([A-Z0-9]{5}-){4}[A-Z0-9]{5}";
System.out.println(str.matches(regex));
}
}
正则的功能:
1)切割功能:使用String类中的split方法。
Java代码:
public class Demo {
public static void main(String[] args) {
//1. A.切割字符串"aa,bb,cc";
String str = "aa,bb,cc";
// String[] strArray = str.split(",");
String[] strArray = str.split("[,]");
for(int i = 0;i < strArray.length ; i++){
System.out.println(strArray[i]);
}
//2.B.切割字符串"aa.bb.cc";
str = "aa.bb.cc";
String[] strArray2 = str.split("[.]");
for(int i = 0 ;i < strArray2.length ; i++){
System.out.println(strArray2[i]);
}
System.out.println("----空格作为分隔符----");
//3..切割字符串“-1 99 4 23”;
//"-1 99 4 23";
str = "-1 99 4 23";
str = "-1 99 4 23";
strArray2 = str.split("[ ]+");
for(int i = 0;i < strArray2.length ; i++){
System.out.println(strArray2[i]);
}
}
}
2) 替换功能: 使用String类中的replaceAll方法。
Java代码:
public class Demo {
public static void main(String[] args) {
String str = "fjirfa;lrqjifjd;kljfa;f";
//将上述字符串中的所有;符号,替换为-符号
System.out.println(str.replaceAll(";", "-"));
//用”#”替换叠:"sdaaafghccccjkqqqqql";
str = "sdaaafghccccjkqqqqql";
String regex = "(.)\\1+";
System.out.println(str.replaceAll(regex, "#"));
//把多个叠词变成一个。用上面的字符串;
System.out.println(str.replaceAll("(.)\\1+", "$1"));
//实际应用:有些论坛不允许发电话,qq号,银行账号等. 把数字用"*"替换
str = "we234rt13245asfklwyoewo4577rewsfd6744232433fafs";
System.out.println(str.replaceAll("\\d+", "*"));
//QQ、电话、账户等都是5位以上的,保留5位以下的
System.out.println(str.replaceAll("\\d{5,}", "*"));
}
}
Java代码:
邮箱校验:
public class Demo {
public static void main(String[] args) {
String str1 = "zhangsan@126.com";
String str2 = "zhang_san@sina.com.cn";
String str3 = "zhang_san@163.cn";
String str4 = "894876368@qq.com";
String regex = "[a-zA-Z_0-9]+@[a-z0-9]+\\.(com|cn|com\\.cn)";
System.out.println(str1.matches(regex));
System.out.println(str2.matches(regex));
System.out.println(str3.matches(regex));
System.out.println(str4.matches(regex));
}
}
6)Math类/Random类/System类:
Math类概述:
Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
成员属性:
PI:比任何其他值都更接近 pi(即圆的周长与直径之比)的 double 值。
E:比任何其他值都更接近 e(即自然对数的底数)的 double 值。
成员方法:
public static int abs(int a):返回 int 值的绝对值。
public static double ceil(double a):返回最小的(最接近负无穷大)double 值,该值大于等于参数,并等于某个整数。
public static double floor(double a):回最大的(最接近正无穷大)double 值,该值小于等于参数,并等于某个整数。
public static int max(int a,int b) min自学:求两个数的最大值
public static double pow(double a,double b):返回第一个参数的第二个参数次幂的值。
public static double random():返回带正号的 double 值,该值大于等于 0.0 且小于 1.0。
public static int round(float a) 参数为double的自学:返回最接近参数的 long。结果将舍入为整数:
public static double sqrt(double a):返回正确舍入的 double 值的正平方根
Random类:
构造函数:
public Random():
public Random(long seed):
成员方法:
nextInt():在int取值范围内的随机数;
nextInt(int n) : 0 >= 数 < n
Java代码:
public class Demo {
public static void main(String[] args) {
Random rdm = new Random();
for(int i = 0;i < 100;i++){
System.out.println("生成一个1--100之间的数:" + (rdm.nextInt(100) + 1));
}
}
}
System类:
System 类包含一些有用的类字段和方法。它不能被实例化。
在 System 类提供的设施中,有标准输入、标准输出和错误输出流;对外部定义的属性和环境变量的访问;加载文件和库的方法;还有快速复制数组的一部分的实用方法。
public static void gc():运行垃圾回收器。
public static void exit(int status):终止当前正在运行的 Java 虚拟机。
public static long currentTimeMillis()
public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
src - 源数组。
srcPos - 源数组中的起始位置。
dest - 目标数组。
destPos - 目标数据中的起始位置。
length - 要复制的数组元素的数量。
7)BigInteger类/BigDecimal类:
BigInteger类:表示任意精度的整数;
构造方法:
public BigInteger(String val):
成员方法:
public BigInteger add(BigInteger val):加法
public BigInteger subtract(BigInteger val):减法
public BigInteger multiply(BigInteger val):乘法
public BigInteger divide(BigInteger val):除法
public BigInteger[] divideAndRemainder(BigInteger val):
返回包含 (this / val) 后跟 (this % val) 的两个 BigInteger 的数组。 商和余数 的数组
当我们需要进行浮点运算时,不要使用float和double基本数据类型,可能会产生丢失精度;
使用:java.math.BigDecimal类:
构造方法:
BigDecimal(String str):推荐使用String参数的构造函数。为什么?使用float或double构造
BigDecimal,由于float和double本身就会丢失精度,所以,构造出来的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): 除法,可以设定精度,以及舍入方式
divisor - 此 BigDecimal 要除以的值。
scale - 要返回的 BigDecimal 商的标度。
roundingMode - 要应用的舍入模式。
8)Date类/DateFormat类/Calendar类:
构造方法:
1、Date():
2、Date(long time):使用一个毫秒数构造一个Date
成员方法:
1、getTime();
2、setTime(long time);
DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间。
使用子类:java.text.SimpleDateFormat
格式化的标记:
y : 年
M : 年中的月份
d : 月中的天数
H :一天中的小时数(0-23)
m : 小时中的分钟数
s : 分钟中的秒数
成员方法:
public final String format(Date date)
public Date parse(String source)
1.Calendar是一个抽象类:
2.可以使用它的子类:GregorianCalendar
也可以调用Calendar的static getInstance()获取一个子类对象:
成员方法:
1.public int get(int field):返回给定日历字段的值
2.public void add(int field,int amount)根据日历的规则,为给定的日历字段添加或减去指定的时间量。
3.public final void set(int year,int month,int date):设置为新的年、月、日信息;
4.public void set(int field,int amount):将给定的日历字段设置为给定值。
Java练习代码:
计算任意一年的二月有多少天
public class Demo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份:");
int year = sc.nextInt();
GregorianCalendar gc = new GregorianCalendar(year,3 - 1,1);
//将日期 -1
gc.add(Calendar.DAY_OF_MONTH, -1);
//获取日期
int day = gc.get(Calendar.DAY_OF_MONTH);
System.out.println(year + " 年的二月份有:" + day + " 天");
}
}