String、char、正則表達式的用法及實例

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * Created by Administrator on 2017/5/16.
 */
public class JavaStringExam {
    public static void main(String[] args) {
        //String 常用方法
        //1、創建String對象
        String a = new String();  //""
        String b = "";
        String c =new String("");


        //2. 字符串編碼轉換, 編碼轉換不可逆,
        String s = "離開家練練";
        try {
            //gbk簡體  gb2312簡體繁體更豐富
            String encodedS =  new String(s.getBytes(),"gb2312");
            System.out.println(encodedS);
        } catch (UnsupportedEncodingException e) {


        }
        //3. 字符串和char[]
        String cha = "123456"; //字符串就是char[]
        char[] charArr = cha.toCharArray(); //字符串直接轉char數組
        String charToString = new String(charArr); //char數組轉字符串
        System.out.println(charToString);


        //4. 字符串常用方法
        String str = "1".concat(".").concat("1");   //concat  拼接字符串 和  + 相同用法
        String str1 = "1"+"."+"1";
        //replace 原char換新char
        String str2 = "1  101/a/".replace("/","(");// ?0次1次  + 1次或多次  *N次   {1}匹配固定次數
        String str3 = "1  101.a.".replaceFirst("\\.",""); //正則表達式替換   \任意字符, 匹配一個任意字符
        String str4 = "1  101\\a\\".replaceAll("\\\\",""); //  正則表達式中" \\" 代表一個"\"


        char strCharAt = "456".charAt(0);
        System.out.println(strCharAt);
        // 1  左比右大
        // 0 相等
        // -1 左比右小   97   98     compareTo每一位字符轉ascII碼,每一位都比較完結果就是compareTo的結果
        int compareResult = "a".compareTo("b");  //String實現了Compareable接口, String的compareTo按照ascII碼比較
        System.out.println(compareResult);


        //內容處理
       boolean isStartA =  "a123".startsWith("a1"); //什麼字符開始
       boolean isUrl =  "baidu.com".endsWith(".com");
      String url =   "http://www.baidu.com";
      boolean hasWWW =  url.contains("www"); //判斷字符串中是否包含www
        //網址判斷
        if(url.contains("www")&&url.startsWith("http:")&&(url.endsWith(".com")||url.endsWith(".cn")||url.endsWith(".net"))){


        }
        //用戶登錄。 "ABC123"
        if("ABC123".equalsIgnoreCase("abc123")){
            //忽略大小寫比較字符串內容
            System.out.println("字符串相等");
        }
       String abc =  "ABC123".toLowerCase(); //字符串轉小寫
       String upABC= "abc123".toUpperCase();//字符串轉大寫


        //indexOf字符串查找
        //沒找到結果 -1
        if("acda".indexOf("a")>-1){
            //判斷字符在字符串中的位置,從左向右找,找第一次出現位置
            System.out.println("acda".indexOf("a"));
        }
        if("acda".lastIndexOf("a")>-1){ //從右向左找。找到一個目標就不繼續查找
            System.out.println("acda".lastIndexOf("a"));
        }


        //isEmpty length()>0判斷字符串非空
        if(!"123123".isEmpty()){
            System.out.println("非空");
        }
        if("123".length()>0){
            System.out.println("非空");
        }
        //字符串的裁剪拼接
        //substring 重載方法1  傳截取開始位置
        String aa = "在計算機中,所有的數據在存儲和運算時都要使用二進制數表示";
        String subResult =   aa.substring(aa.indexOf(",")+1);
        //所有的數據在存儲和運算時都要使用二進制數表示
        System.out.println(subResult);
        //substring  兩個參數時, 截取結果。 前面要,後面不要, 第一個參數位置返回,第二個參數位置不會返回
       String subResult1 = aa.substring(0,aa.indexOf(","));
        System.out.println(subResult1);
        //字符串的拆分
        String lstr = "1|2|3|4|5";//按| 拆分   每看到一個|  就把|前面字符放數組中
      String[]  strs =   lstr.split("\\|"); //   |  在正則表達式中表示  或
        System.out.println(Arrays.toString(strs));


        //字符串格式化, 數據庫讀取的字符串,可能有很多空格
        String  trimed =  "   sfd   sdf     ".trim(); //格式化字符串,去掉字符串左右空格
        System.out.println(trimed);


        //正則表達式處理
        //正則表達式驗證
        String userNameRegex =  "[A-Za-z0-9_\\-\\u4e00-\\u9fa5]{6,16}";
        String userName = "水電費123";
        boolean isMatch = Pattern.matches(userNameRegex,userName);  //判斷userName是否被 正則匹配
        if(isMatch){
            System.out.println("用戶名符合要求");
        }
       //正則表達式截取
        String  web = "<dl>\n" +
                "\t\t<dt>小托馬斯地表最強</dt>\n" +
                "\t\t<dd>http://n.sinaimg.cn/sports/2_img/upload/cf0d0fdd/20170516/d26_-fyfeutq0578202.jpg</dd>\n" +
                "        \t\t<dd>http://www.sinaimg.cn/dy/slidenews/2_t160/</dd>\n" +
                "\t\t<dd>http://www.sinaimg.cn/dy/slidenews/2_t50/</dd>\n" +
                "\t\t<dd>2017年05月16日 11:02</dd>\n" +
                "\t\t<dd>北京時間5月16日,奇才與凱爾特人的系列賽進入搶七,結果凱爾特人115比105力克奇才,從而以總比分4比3淘汰凱爾特人晉級東部決賽;小托馬斯29分12助攻,凱利-奧利尼克26分,約翰-沃爾18分11助攻,布拉德利-比爾38分。</dd>\n" +
                "\t\t<dd><a href=\"http://comment5.news.sina.com.cn/comment/skin/default.html?channel=ty&newsid=slidenews-786-2144983\">評論</a></dd>\n" +
                "        \t\t<dd>2144983</dd>\n" +
                "\t</dl>";
        String regex ="<dl>\\s*<dt>[\\W\\w]*</dt>\\s*<dd>([\\W\\w]*)</dd>\\s*</dl>";
       if(Pattern.matches(regex,web)) {
           //正確匹配的處理
           //創建Matcher對象  Matcher匹配器    Pattern正則表達式對象
           Matcher matcher = Pattern.compile(regex).matcher(web);
           if (matcher.find()) { //能找到 正則表達式中 ()匹配的結果  ()一個分組
               String webContent = matcher.group(1);
               System.out.println(webContent);
           }
       }
    }
}
發佈了54 篇原創文章 · 獲贊 68 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章