java —— String 類

來源:互聯網
上載者:User

標籤:相同   串連   ini   字串長度   bool   字元   alt   gpo   com   

一、String 常用方法
  1 import java.util.Arrays;  2   3 public class StringDemo {  4     public static void main(String[] args) {  5         String str = "String 方法";  6         // 判斷字串是否為空白  7         str.isEmpty();  8         // 擷取字串長度  9         str.length(); 10         // 刪掉開頭和結尾的空格 11         str.trim(); 12  13         // 在字串中 0 到結尾 尋找第一個找到的字元或字串索引位置,沒有返回 -1 14         // public int indexOf(String str) 15         // public int indexOf(int ch) 16         int index = str.indexOf("ing"); // 3 17         index = str.indexOf(‘i‘); // 3 18         // fromIndex 從這個索引(包含)進行位置尋找 ,也就是 fromIndex 到 結尾 19         // public int indexOf(int ch, fromIndex) 20         // public int indexOf(String ch, fromIndex) 21         index = str.indexOf(‘i‘, 3); // 3 22         // lastIndexOf 與 indexOf 方法類似,從後面向 索引 0 尋找 23         index = str.lastIndexOf(‘i‘); 24  25         // 字串中是否包含指定的字元序列,String 就是實現了介面 CharSequence 26         // public boolean contains(CharSequence s) 27         // 內部是實現: return indexOf(s.toString()) > -1; 28         boolean ct = str.contains("ing"); // true 29  30         // 判斷字串是否以給定子字串開頭 31         // public boolean startsWith(String prefix) 32         boolean st = str.startsWith("Str");// true 33         // public boolean startsWith(String prefix, int toffset) 34         // toffset 表示開始的位置 35         st = str.startsWith("Str", 2); // false 36         // 判斷字串是否以給定子字串結尾 37         // public boolean endsWith(String suffix) 38         boolean ed = str.endsWith("法"); // true 39  40         // 與其他字串比較,看內容是否相同 41         // public boolean equals(Object anObject) 42  43         // 忽略大小寫,與其他字串進行比較,看內容是否相同 44         // public boolean equalsIgnoreCase(String anotherString) 45         "abc".equals("Abc"); // false 46         "abc".equalsIgnoreCase("ABc"); // true 47  48         // 轉大小寫,返回新字串,原字串不變 49         str.toLowerCase(); // 小寫 50         str.toUpperCase(); // 大寫 51  52         // 字串比較大小,按照編碼值 53         // public int compareTo(String anotherString) 54         "abc".compareTo("Abc"); // => return ‘a‘ - ‘A‘ 字元逐一進行比較 55         // 忽略字母大小寫進行比較 (都轉大寫再比較) 56         // public int compareToIgnoreCase(String str) 57  58         // 截取字串返回新的字串 59         // public String substring(int beginIndex) 60         // public String substring(int beginIndex, int endIndex) 61         // beginIndex 開始的索引 endIndex 結束的索引 62         // beginIndex 包括在截取的範圍內,而endIndex不包括 63         String sub = str.substring(1); // tring 方法 64         String sube = str.substring(1, 2); // t 65  66         // 字串串連,返回當前字串和參數字串合并後的字串,原字串不變 67         // public String concat(String str) 68         String cc = str.concat(" --> concat 串連方法"); 69         // String 方法 --> concat 串連方法 70  71         // 字串替換 ,返回新字串,原字串不變 72         // 替換單個字元 73         // public String replace(char oldChar, char newChar) 74         String rp = str.replace(‘方‘, ‘圓‘);// String 圓法 75         // 替換字元序列 76         // public String replace(CharSequence target, CharSequence replacement) 77         rp = str.replace("String", "replace"); // replace 方法 78         // 利用正則選擇需要替換的字元 79         // regex --> Regex的字串 80         // replacement 替換的元素 81         // public String replaceAll(String regex, String replacement) 82         rp = str.replaceAll("[a-z]", "i"); // Siiiii 方法 83         // 表示:將匹配到每個小寫字母,變為 i 84         // replaceFirst 改變第一次符合要求的字串 85         rp = str.replaceFirst("[a-z]", "i"); 86  87         // 分隔字串,返回分隔後的子字串數組,原字串不變 88         // regex 根據字串分隔,匹配到的字串將被移除 89         // public String[] split(String regex) 90         String[] strs = "HELLO WORLD THIS NIGHT".split(" "); 91         System.out.println(Arrays.asList(strs)); 92         // [HELLO, WORLD, THIS, NIGHT] 93         strs = "HELLO WORLD THIS NIGHT".split("H"); 94         System.out.println(Arrays.asList(strs)); 95         // [, ELLO WORLD T, IS NIG, T] 96  97         // limit 限制分隔的次數(limit - 1) 98         // 也可以理解匹配到 regex 的次數為 limit - 1 99         // 如果限制的次數(limit - 1)大於實際分隔的次數100         // 或者 limit - 1  <  0101         // 那就按正常分隔無限制。102         // "HELLO WORLD THIS NIGHT".split(" ") 這樣的分隔103         // public String[] split(String regex, String limit);104         for (int i = -1; i < 6; i++) {105             strs = "HELLO WORLD THIS NIGHT".split(" ", i);106             System.out.println(Arrays.asList(strs));107         }108         /**109          *           實際可以分隔3次,可得到數組長度為4110          * 111          * -1 [HELLO, WORLD, THIS, NIGHT] 112          *  0 [HELLO, WORLD, THIS, NIGHT] 113          *  1 [HELLO WORLD THIS NIGHT] 114          *  2 [HELLO, WORLD THIS NIGHT] 115          *  3 [HELLO, WORLD, THIS NIGHT]116          *  4 [HELLO, WORLD, THIS, NIGHT] 117          *  5 [HELLO, WORLD, THIS, NIGHT]118          */119     }120 121 }
二、String 的構造方法

 

java —— 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.