First, regular expressions
1, Overview: it is used to describe a match or series of matches a string syntax rules; in fact, a rule, have their own particular application.
2, is composed of the regular expression
(1), character
expression | On behalf of the meaning of |
---|---|
x | Character x. Example: 'a' represents a character |
\ | Backslash characters. |
\n | New line (line feed) ( '\ u000A') |
\r | Carriage return ( '\ u000D') |
[abc] | a, b or C (Simple Profile) |
[^abc] | Any characters, in addition to a, b or C (negative) |
[A-zA-Z] | a to z or A to Z, including two letters (range) |
[0-9] | Characters 0-9 are included |
. | Any character. I is. Character itself, how it represent?. |
\d | Digital: [0-9] |
\w | Word characters: [a-zA-Z_0-9] |
(2), the boundary matcher
expression | On behalf of the meaning of |
---|---|
^ | Beginning of the line |
$ | End of the line |
\b | Word boundary is not a word character place. |
(3), Greedy quantifier
expression | On behalf of the meaning of |
---|---|
X? X | Or once there is no such "" empty string is not |
X* X | Zero or more times greater than equal to 1 are counted multiple times |
X+ X | One or more times |
X{n} X | Exactly n times |
X{n,} X | At least n times |
X{n,m} X | At least n times, but not more than m times |
(4), for example
String regx="abc";//定义正则表达式
regx="\\|"; // \\ 转意字符
regx="[abcdef]"; // 是我这个中括号出现的某一个字符
regx="[12345]";
regx="[12344abcdef]";
regx="[0-9]";
regx="[a-z]";
regx="[A-Z]";
regx="[a-zA-Z0-9]";
regx="[^abc]"; //^ 不是我这个列表里面的一个字符
regx=".."; //匹配单个任意字符
regx="\\.";// \\. 代表点本身
regx="\\d"; //等价于 [0-9]
regx="^\\w$";// 等价于 [a - z_A - Z0 - 9]
//数量词
regx="[a-z]*"; //* 0次或多次 空串 就是0次 1个也算多次
regx="[0-9a-zA-Z]+"; // + 一次 或多次
regx="[a-z]?";//一次 或一次也没有
regx="[a-z]{5}"; //正好几次
regx="[a-z]{5,}"; //至少几次
regx="[a-z]{5,15}";// 大于等与5 小于等于 15
regx = "[1-9][0-9]{4,14}"; //正则表达式 正则表达式的语法
boolean b = "abcd".matches(regx);
3, the regular expression functions
(1), determining function
- public boolean matches(String regex)
public class Test {
public static void main(String[] args) {
//需求:校验邮箱 org cn net com
//[email protected]
//定义邮箱的正则 6 ~18 个字符,可使用字母、数字、下划线,需以字母开头
String emaliRegx="[a-zA-Z]\\w{5,17}@[1-9a-z]{2,16}\\.(com|net|cn|org)";
boolean matches = "[email protected]".matches(emaliRegx);
System.out.println(matches);
}
}
(2) intercept function
- public String[] split(String regex);
public classTest {
public static void main(String[] args) {
String str="username=张三";
String s = str.substring(0, str.indexOf("="));
System.out.println(s); //username
String substring = str.substring(str.indexOf("=")+1);
System.out.println(substring); //张三
String str2 = "username=|#==@张三=|||#==@password=123456";
String[] strings = str2.split("[=\\|#@]+");
System.out.println(strings[0]); //username
System.out.println(strings[1]); //张三
System.out.println(strings[2]); //password
System.out.println(strings[3]); //123456
}
}
(3) replacement function
- public String replaceAll(String regex,String replacement)
public class Test {
public static void main(String[] args) {
String s = "我爱学习编程".replace("编程", "java");
System.out.println(s);
String str="我爱==#@@@=学习#===|||java";
String s1 = str.replaceAll("[=#@\\|]+", "");//根据正则替换
System.out.println(s1);
//去掉中间空格
String s2 = "我 爱 学 习 j ava";
String s3 = s2.replaceAll(" +", "");
System.out.println(s3);
}
}
Two, System class
1 Overview: System class contains several useful class fields and methods. It can not be instantiated.
2, members of the method
- public static void gc () // call the garbage collector
- public static void exit (int status) // quit java virtual machine 0 0 non-normal exit is abnormal exit
- public static long currentTimeMillis () // Get the current time value in milliseconds
in standard "input stream. This stream is already open and ready to provide input data. Typically, this corresponds to the keyboard input stream
- Scanner sc = new Scanner(in);
out "standard" output stream. This stream is already open and ready to accept output data. Typically, this stream corresponding to the display output
- PrintStream out = System.out;
3, members of the class using the method System
public class Test {
public static void main(String[] args) {
//循环耗时
//当前系统的毫秒值,从 1970 01 01 00:00:00 来参照的
long start = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
System.out.println(i);
}
long end = System.currentTimeMillis();
System.out.println("循环耗时"+(end-start)+"毫秒");
//获取指定的环境变量值。
String java_home = System.getenv("JAVA_HOME");
System.out.println(java_home);
String path = System.getenv("Path");
System.out.println(path);
}
}
Three, the BigDecimal class
1 Overview: As at the time of operation, float type and double precision can easily be lost, therefore, in order to accurately represent, floating point calculation, Java provides BigDecimal. Immutable, arbitrary-precision signed decimal numbers.
2, the configuration method
- public BigDecimal(String val)
3, member methods
- 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) // scale back several decimal places, roundingMode trade-offs such as rounding mode
4, and member constructor method using a class BigDecimal
public class Test {
public static void main(String[] args) {
BigDecimal a = new BigDecimal("10");
BigDecimal b = new BigDecimal("3");
BigDecimal add = a.add(b); //加法
BigDecimal subtract = a.subtract(b);//减法
BigDecimal multiply = a.multiply(b);//乘法
//如果不能整除,得指定这个保留的位数,以及取舍模式
BigDecimal divide = a.divide(b,5,BigDecimal.ROUND_CEILING);//除法
System.out.println(add.toString());//13
System.out.println(subtract);//7
System.out.println(multiply);//30
System.out.println(divide);//3.33334
}
}
Four, Date Class
1. Overview: class Date represents a specific moment, with millisecond precision.
2, the configuration method
- public Date()
- public Date (long date) // converts a type of milliseconds long as a date object
3, member methods
- public long getTime (): Get a subject object date values ms
- public void setTime (long time): date object is set to a specified value in milliseconds
4, using for example
public classTest {
public static void main(String[] args) {
Date date = new Date();
long time = date.getTime();//获取系统当前的毫秒值
System.out.println(time);
Date date2 = new Date();
date2.setTime(1000 * 60 * 60 * 24 * 5); //在计算机元年上增加相应的毫秒值
System.out.println(date2);
}
}
Five, Calendar class
1, Overview: Calendar class is an abstract class that can not be directly new object, you can come get his target through his a static member method getInstance (), which is a particular moment with a group such as YEAR, MONTH, DAY_OF_MONTH transition between the calendar field HOUR provide some method, and provides some methods for manipulating the calendar field.
2, members of the method
- When public static Calendar getInstance () uses the default time zone and locale to get a calendar object
- public int get (int field) to obtain a given calendar field value corresponding to the field provided by the field to come and collect Calendar
3, add () and set () method
- The amount of time public void add (int field, int amount) according to the calendar rules, for a given calendar add or subtract a specified field
- public final void set (int year, int month, int date) set the calendar date time
4, using the method of the Calendar class
public class MyDemo2 {
public static void main(String[] args) {
Calendar instance = Calendar.getInstance();
instance.add(Calendar.YEAR,-2);
System.out.println(instance.get(Calendar.YEAR));
Calendar instance1 = Calendar.getInstance();
instance1.set(Calendar.YEAR,2016);
System.out.println(instance1.get(Calendar.YEAR));
}
}