正則表達式,Pattern和Matcher類,Math類,Random類。System類,BigDecimal類

正則表達式

  1. 概念:就是人爲規定的一種規則
  2. 常用的組成規則

A:字符
x 字符 x。舉例:‘a’表示字符a
\ 反斜線字符。
\n 新行(換行)符 (’\u000A’)
\r 回車符 (’\u000D’)
B:字符類
[abc] a、b 或 c(簡單類)
[^abc] 任何字符,除了 a、b 或 c(否定)
[a-zA-Z] a到 z 或 A到 Z,兩頭的字母包括在內(範圍)
[0-9] 0到9的字符都包括
C:預定義字符類
. 任何字符。我的就是.字符本身,怎麼表示呢? .
\d 數字:[0-9]
\w 單詞字符:[a-zA-Z_0-9]
在正則表達式裏面組成單詞的東西必須有這些東西組成
D:邊界匹配器
^ 行的開頭
$ 行的結尾
\b 單詞邊界
就是不是單詞字符的地方。
舉例:hello world?haha;xixi
E:Greedy 數量詞
X? X,<=1
X* X,>=0
X+ X,>=1
X{n} X, n
X{n,} X, n
X{n,m} X, >=n&&<=m

  1. 判斷手機號碼是否滿足規則
    11位且不能以0開頭的數字字符串

String s="[1-9][0-9]{10}";
boolean b = “18791050779”.matches(s);
System.out.println(b);

  1. 校驗郵箱
    [email protected]以此郵箱爲例

String s="[a-zA-Z]\w{5,17}@163\.(com|net|cn|org)";
boolean matches = “[email protected]”.matches(s);
System.out.println(matches);

  1. 正則表達式的分割功能 split()方法

"91 27 46 38 50 21 11 1 121"將這個字符串排序並輸出。

String str=“91 27 46 38 50 21 11 1 121”;
String[] arr = str.split(" +");
System.out.println(Arrays.toString(arr));//得到的是集合
Arrays.sort(arr);

int[] ints = new int[arr.length];
for (int i = 0; i < ints.length; i++) {
ints[i] = Integer.parseInt(arr[i]);//將集合裏的元素全部轉換爲int類型的數,得到數組
}
Arrays.sort(ints);
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < ints.length; i++) {
stringBuilder.append(ints[i]).append(" ");
}
String trim = stringBuilder.toString().trim();//借用trim和StringBuilder實現數組排序
System.out.println(trim);

Pattern和Matcher類

  1. 典型的調用順序是
    Pattern p = Pattern.compile(“a*b”);
    Matcher m = p.matcher(“aaaaab”);
    boolean b = m.matches();

  2. da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu?將這個字符串中三個字母的字符打印出來

String str=“da jia ting wo shuo, jin tian yao xia yu, bu shang wan zi xi, gao xing bu?”;
String regx="\b[a-z]{3}\b";
String[] strings = str.split("[^a-z]+");//除去空字符的字母集合
System.out.println(Arrays.toString(strings));
for (int i = 0; i < strings.length; i++) {
if(strings[i].length()==3){
System.out.println(strings[i]);
}
}

Math類

  1. Math類概述
    Math 類包含用於執行基本數學運算的方法,如初等指數、對數、平方根和三角函數。

  2. 成員變量
    public static final double E : 自然底數
    public static final double PI: 圓周率

  3. 成員方法
    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 int min(int a, int b) 獲取最小值
    public static double pow(double a,double b) 獲取a的b次冪
    public static double random() 獲取隨機數 返回帶正號的 double 值,該值大於等於 0.0 且小於 1.0。
    public static int round(float a) 四捨五入
    public static double sqrt(double a)獲取正平方根

Random類

  1. 構造方法
    public Random() 沒有給定種子,使用的是默認的(當前系統的毫秒值)
    public Random(long seed) 給定一個long類型的種子,給定以後每一次生成的隨機數是相同的

  2. 成員方法
    public int nextInt()//沒有參數 表示的隨機數範圍 是int類型的範圍
    public int nextInt(int n)//可以指定一個隨機數範圍
    void nextBytes(byte[] bytes) 生成隨機字節並將其置於用戶提供的空的 byte 數組中。

System類

  1. System類的概述
    System 類包含一些有用的類字段和方法。它不能被實例化。

  2. 成員方法
    public static void gc()//調用垃圾回收器
    public static void exit(int status)//退出java虛擬機 0 爲正常退出 非0爲 異常退出
    public static long currentTimeMillis()//獲取當前時間的毫秒值

BigDecimal類

  1. 精度能達到你的要求,就是精度高

  2. 構造方法
    public BigDecimal(String val)

  3. 成員方法
    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 小數點後面保留幾位
    // roundingMode 取捨模式 比如四捨五入

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章