day13 Java学习(常见对象正则表达式)
常見對象(正則表達式)
? ? 概述: 指一個用來描述或者匹配一系列符合某個語法規(guī)則的字符串的單個字符串。(其實就是一種規(guī)則)
? ?檢驗方法: matches()
? ? 格式:“*******”.matches(變量名)
?
? 規(guī)則:
? ? ?.? ? ?任意字符
? ? ?. ??? 任意字符:一次或者一次也沒有
? ???. *?? 任意字符:零次或者多次
? ? ?. +?? 任意字符:一次或者多次
? ? ?. { n }?任意字符:恰好 n 次
? ? ?. { n, }?任意字符:至少 n 次
? ? ?. { n,m }?任意字符:至少 n 次 ,但是不超過 m 次?
? ? \d? ? ?數字:[ 0 - 9 ]
? ? \D? ? 非數字 :[ ^ 0-9 ]
? ? \s? ? ?空白字符 :[? \t\n\x0B\f\r ]
? ? \S? ? ?非空白字符 :[? ^\s ]
? ? \w? ? ?單詞字符:[ a-zA-Z_0-9 ]
? ? \W? ? ?非單詞字符:[?^\w?]
?
?正則表達式的分割功能:
? ? ? ?String類的方法: split ( String regex )
??正則表達式的替換功能:
? ? ? ?String? replaceAll ( String regex , String replacement )
?
?常見對象(Pattern和Matcher)
? ? ? ? 調用順序:
? ? ? ? ? ? ??Pattern p =?Pattern.compile ( a* b)?
? ? ? ? ? ? ??Matcher m = p.matcher( " aaaab")
? ? ? ? ? ? ? boolean b = m.matcher();
? ??
String st = "號碼:15975555555 或者17888888888,17878888888";String regex = "1[3578]\\d{9}"; // 手機的正則表達式Pattern p = Pattern.compile(regex); // 獲取到正則表達式Matcher m = p.matcher(st); // 獲取匹配器while (m.find()) { // find()在字符串中找符合正則表達式的字符串。找到為true,找不到為false System.out.println(m.group()); // group()找到匹配的都獲取出來} 正則表達式獲取手機號碼功能?
常見對象(Math 類)
? Math: 用于執(zhí)行基本數學運算的方法,如初等指數,對數,平方根,三角函數。
??
System.out.println(Math.abs(-15.2)); // Math.abs() 取絕對值。System.out.println(Math.ceil(15.3)); // Math.ceil() 結果向上取整,是double類型。System.out.println(Math.floor(12.9)); // Math.ceil() 結果向下取整,是double類型。System.out.println(Math.max(-16.2, 16.1)); // Math.max() 比較結果大小,取最大值System.out.println(Math.min(-16.2, 16.1)); // Math.min() 比較結果大小,取最小值System.out.println(Math.pow(1,10 )); // Math.pow() 取前面位置的次方,是double類型。System.out.println(Math.random()); //隨機生成0.0到1.0之間的小數,包括0.0,不包括1.0。System.out.println(Math.round(15.8)); //四舍五入。System.out.println(Math.sqrt(9)); //參數的平方根。 成員方法?
?常見對象(Random類)
? ? Random:此類用于生產隨機數。
??
Random r = new Random(); //無參的。int a = r.nextInt(); //隨機生成正負數。 System.out.println(a); int x = r.nextInt(100); // 隨機生成0到n-1d的隨機數, System.out.println(x);Random d = new Random(1000); //有參的。int b= d.nextInt(); //如果輸出變量不變隨機生成的數也不變。System.out.println(b); 成員方法?
?常見對象(System類)
? ? ?System:包含一些有用的類字段和方法。它不能被實例化。?
? ? ?在一個源文件中不允許定義兩個用 public 修飾的類
?
System.exit(0); //非0狀態(tài)是異常終止,推出jvmlong start = System.currentTimeMillis(); // 獲取當前時間的毫秒值。for (int i = 0; i < 1000; i++) {System.out.println("*");}long end = System.currentTimeMillis();System.out.println(end - start); // 計算當前方法所用的時間。int[] src = { 22, 55, 44, 6 };int[] dest = new int[8];System.arraycopy(src, 0, dest, 0, src.length); // 將數組內容拷貝 成員方法?
?
?
常見對象(BigInteger類)
? ?概述: 可以讓超過 Integer 范圍內的數據進行運行。
? 構造方法: public?BigInteger( String val )
??
BigInteger b1 = new BigInteger("2"); BigInteger b2 = new BigInteger("1");System.out.println(b1.add(b2)); // +System.out.println(b1.subtract(b2)); // -System.out.println(b1.multiply(b2)); // * System.out.println(b1.divide(b2)); // /(除)BigInteger [] arr= b1.divideAndRemainder(b2);//取除數h和余數for (int i = 0; i < arr.length; i++) {System.out.println(arr[i]);} 成員方法?
?
常見對象(BigDecimal類)
? ? 概述: 由于在運算時,floa 和 double 很容易丟失精度,所以為了精確表示,Java提供了BigDecimal。? ? ? ? ? ??
? ? 構造方法: public BigDecimal( String val )
? ??
BigDecimal b=new BigDecimal("2"); //通過構造傳入字符串方式,開發(fā)時推薦BigDecimal b1=new BigDecimal("1.1");System.out.println(b.subtract(b1));//通過類名 . 調用valueOf()方法BigDecimal a = BigDecimal.valueOf(7); // 這種方式在開發(fā)也是推薦的BigDecimal a1=BigDecimal.valueOf(1.1);System.out.println(a.subtract(a1)); 成員方法?
?
?
?
?
?
?
? ? ?
轉載于:https://www.cnblogs.com/feng0001/p/10909524.html
總結
以上是生活随笔為你收集整理的day13 Java学习(常见对象正则表达式)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一起来学Spring Cloud | 第
- 下一篇: 某个exe文件打不开怎么办 该exe文件