03-String常用方法

來源:互聯網
上載者:User

標籤:定位   equal   表單   equals   out   OLE   pre   boolean   nbsp   

1.擷取方法

        /*         * 編輯:劉詩華         int  length()  擷取字串的長度         char charAt(int index) 擷取特定位置的字元 (角標越界)         int  indexOf(String str) 擷取特定字元的位置(overload)         int  lastIndexOf(int ch) 擷取最後一個字元的位置        */                String str="做人,多一份真誠,少一份套路";                //1.擷取字串長度個數        int length = str.length();        System.out.println(length);  //14個字元                //2.擷取指定索引的字元        char c = str.charAt(6);        System.out.println(c);  //真                //3.搜尋指定字串首次出現位置索引          int index = str.indexOf("一");        System.out.println(index);   // 第一個 "-" 在第下標為 4的位置                //4.搜尋指定字串最後一次出現的位置        int lastIndex = str.lastIndexOf("一");        System.out.println(lastIndex);   //10

 

02.判斷方法

        String str="粉紅色的回憶-韓寶儀";                //1.某指定字串開頭,startsWith()在開發表單的多選擷取資料上很有用        boolean b1 = str.startsWith("粉紅");  //返加一個boolean值,如果是以 [粉紅] 字串開頭,則返 true 如果不是返回 false        //返回 true        System.out.println(b1);                //2.判斷是否以指定字串結尾        boolean b2=str.endsWith("韓寶儀麼");        System.out.println(b2); //顯然不是以 [韓寶儀麼] 結尾  所以返回 false                //3.判斷字串變數是否為空白        String str3="";  //是內容為空白,不是引用為空白        boolean b3 = str3.isEmpty();        System.out.println(b3); //true                //4.判斷是否包含指定字元        boolean b4=str.contains("回憶");        System.out.println(b4);                //5.判斷兩個字串變數內容是否相等,比較的是內容,String類已經重寫父類方法,所以不再比較是內容地址是否相等        String userName="root";        boolean b5=userName.equals("root");        System.out.println(b5);                //6.忽略大小寫是否相等        boolean b6 =userName.equalsIgnoreCase("RooT");        System.out.println(b6);  //true

 

03-String常用方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.