Java基礎_0308:String類的常用方法

來源:互聯網
上載者:User

標籤:temp   元素   空格   執行   contains   contain   efi   字串串連   index   

取出指定索引的字元 —— 使用charAt()方法
public class StringDemo {    public static void main(String args[]) {        String str = "hello";       // 定義字串對象        char c = str.charAt(0);     // 截取第一個字元        System.out.println(c);      // 輸出字元    }}程式執行結果:h
字元數組與字串的轉換
public class StringDemo {    public static void main(String args[]) {        String str = "hello";       // 定義字串        char[] data = str.toCharArray(); // 將字串變為字元數組        for (int x = 0; x < data.length; x++) { // 迴圈輸出每一個字元            System.out.print(data[x] + "、");        }    }}程式執行結果: h、e、l、l、o、
將字串轉大寫
public class StringDemo {    public static void main(String args[]) {        String str = "hello“;       // 字串由小寫字母組成        char[] data = str.toCharArray();    // 將字串變為字元數組        for (int x = 0; x < data.length; x++) { // 改變每一個字元的編碼值            data[x] -= 32;        }        System.out.println(new String(data));   // 將全部字元數組變為String        System.out.println(new String(data, 1, 2)); // 將部分字元數組變為String    }}程式執行結果:HELLOEL
給定一個字串,要求判斷其是否由數字組成。
public class StringDemo {    public static void main(String args[]) {        String str = "123423432";        if (isNumber(str)) {            System.out.println("字串由數字組成!");        } else {            System.out.println("字串由非數字組成!");        }    }    public static boolean isNumber(String temp) {        char[] data = temp.toCharArray();// 將字串變為字元數組,可以取出每一位字元進行判斷        for (int x = 0; x < data.length; x++) {// 迴圈判斷            if (data[x] > ‘9‘ || data[x] < ‘0‘) {// 不是數字字元範圍                return false;// 後續不再判斷            }        }        return true;    // 如果全部驗證通過返回true    }}程式執行結果: 字串由數字組成!
觀察字串與位元組數組的轉換
public class StringDemo {    public static void main(String args[]) {        String str = "helloworld";  // 定義字串        byte[] data = str.getBytes();   // 將字串變為位元組數組        for (int x = 0; x < data.length; x++) {            data[x] -= 32;  // 將小寫字母變為大寫形式        }        System.out.println(new String(data)); // 全部轉換        System.out.println(new String(data, 5, 5));// 部分轉換    }}程式執行結果:HELLOWORLD(“new String(data)”語句執行結果)WORLD(“new String(data, 5, 5)”語句執行結果)
相等判斷
public class StringDemo {    public static void main(String args[]) {        String stra = "Hello"; // 執行個體化字串對象        String strb = "hELLO"; // 執行個體化字串對象        System.out.println(stra.equals(strb));  // 比較結果:false        System.out.println(stra.equalsIgnoreCase(strb));  // 比較結果:true    }}程式執行結果:false(“stra.equals(strb)”語句執行結果)true(“stra.equalsIgnoreCase(strb)”語句執行結果)
觀察compareTo()方法
public class StringDemo {    public static void main(String args[]) {        String stra = "Hello";  // 定義字串對象        String strb = "HEllo";  // 定義字串對象        System.out.println(stra.compareTo(strb)); // 32,大於0        if (stra.compareTo(strb) > 0) {// 可以利用大小等於0的方式來判斷大小            System.out.println("大於");        }    }}程式執行結果:32(“stra.compareTo(strb)”語句執行結果,比較的是兩個字串的編碼數值)大於(“System.out.println("大於");”語句執行結果)
使用indexOf()等功能尋找
public class StringDemo {    public static void main(String args[]) {        String str = "helloworld";      // 執行個體化字串對象        System.out.println(str.indexOf("world"));   // 返回滿足條件單詞的第一個字母的索引        System.out.println(str.indexOf("l"));   // 返回的是第一個尋找到的子字串位置        System.out.println(str.indexOf("l", 5));    // 從第6個元素開始尋找子字串位置        System.out.println(str.lastIndexOf("l"));   // 從後開始尋找指定字串的位置    }}程式執行結果:5(“System.out.println(str.indexOf("world"));”語句執行結果)2(“System.out.println(str.indexOf("l"));”語句執行結果)8(“System.out.println(str.indexOf("l", 5));”語句執行結果)8(“System.out.println(str.lastIndexOf("l"));”語句執行結果)
利用indexOf()方法判斷子字串是否存在
public class StringDemo {    public static void main(String args[]) {        String str = "helloworld“;  // 字串對象        if (str.indexOf("world") != -1) { // 能找到子字串            System.out.println("可以查詢到資料。");        }    }}程式執行結果:可以查詢到資料。
使用contains()方法判斷子字串是否存在
public class StringDemo {    public static void main(String args[]) {        String str = "helloworld“;      // 字串對象        if (str.contains("world")) {    // 子字串存在            System.out.println("可以查詢到資料。");        }    }}程式執行結果: 可以查詢到資料。
開頭或結尾判斷
public class StringDemo {    public static void main(String args[]) {        String str = "##@@hello**";     // 字串對象        System.out.println(str.startsWith("##"));// 是否以“##”開頭        System.out.println(str.startsWith("@@", 2));    // 從第2個索引開始是否以“@@”開頭        System.out.println(str.endsWith("**"));// 是否以“**”結尾    }}程式執行結果:true(“str.startsWith("##")”語句執行結果)true(“str.startsWith("@@", 2)”語句執行結果)true(“str.endsWith("**")”語句執行結果)
範例:觀察替換的結果
public class StringDemo {    public static void main(String args[]) {        String str = "helloworld" ;         // 定義字串        String resultA = str.replaceAll("l","_") ;  // 全部替換        String resultB = str.replaceFirst("l","_") ;    // 替換首個        System.out.println(resultA) ;        System.out.println(resultB) ;    }}程式執行結果:he__owor_d(“System.out.println(resultA)”語句執行結果)he_loworld(“System.out.println(resultB)”語句執行結果)
驗證字串操作
public class StringDemo {    public static void main(String args[]) {        String str = "helloworld";      // 定義字串        String resultA = str.substring(5);  // 從指定索引截取到結尾        String resultB = str.substring(0, 5);   // 截取部分子字串        System.out.println(resultA);        System.out.println(resultB);    }}程式執行結果:world(“System.out.println(resultA)”語句執行結果)hello(“System.out.println(resultB)”語句執行結果)
進行全部拆分
public class StringDemo {    public static void main(String args[]) {        String str = "hello yootk nihao mldn";// 定義字串,中間使用空格作為間隔        String result[] = str.split(" ");   // 字串拆分        for (int x = 0; x < result.length; x++) {   // 迴圈輸出            System.out.print(result[x] + "、");        }    }}程式執行結果:hello、yootk、nihao、mldn、
拆分為指定的個數
public class StringDemo {    public static void main(String args[]) {        String str = "hello yootk nihao mldn";  // 定義字串,中間使用空格作為間隔        String result[] = str.split(" ",2);     // 字串拆分        for (int x = 0; x < result.length; x++) {   // 迴圈輸出            System.out.println(result[x]);        }    }}程式執行結果:helloyootk nihao mldn
複雜拆分
public class StringDemo {    public static void main(String args[]) {        String str = "張三:20|李四:21|王五:22“;// 定義字串        String result[] = str.split("\\|"); // 第一次拆分        for (int x = 0; x < result.length; x++) {            String temp[] = result[x].split(":");   // 第二次拆分            System.out.println("姓名:" + temp[0] + ",年齡:" + temp[1]);        }    }}程式執行結果:姓名:張三,年齡:20姓名:李四,年齡:21姓名:王五,年齡:22
字串串連
public class StringDemo {    public static void main(String args[]) {        String str = "hello".concat("world") ;  // 等價於“+”        System.out.println(str) ;    }}程式執行結果:helloworld
範例:轉小寫與大寫操作
public class StringDemo {    public static void main(String args[]) {        String str = "(*(*Hello(*(*" ;      // 定義字串        System.out.println(str.toUpperCase()) ; // 轉大寫後輸出        System.out.println(str.toLowerCase()) ; // 轉小寫後輸出    }}程式執行結果:(*(*HELLO(*(*(“System.out.println(str.toUpperCase())”語句執行結果)(*(*hello(*(*(“System.out.println(str.toLowerCase())”語句執行結果)
去掉左右空格
public class StringDemo {    public static void main(String args[]) {        String str = "   hello   world  ";  // 定義字串,包含空格        System.out.println("【" + str + "】");// 原始字串        System.out.println("【" + str.trim() + "】");// 去掉空格後的字串,中間的空格保留    }}程式執行結果:【   hello   world  】【hello   world】
取得字串長度
public class StringDemo {    public static void main(String args[]) {        String str = "helloworld";  // 定義字串        System.out.println(str.length());// 取得字串長度    }}程式執行結果:10
範例:判斷是否為空白字串
public class StringDemo {    public static void main(String args[]) {        String str = "helloworld";      // 定義字串        System.out.println(str.isEmpty());  // 判斷字串對象的內容是否為空白字串(不是null)        System.out.println("".isEmpty());    // 判斷字串常量的內容是否為空白字串(不是null)    }}程式執行結果:false(“System.out.println(str.isEmpty())”語句執行結果)true(“System.out.println("".isEmpty())”語句執行結果)
實現首字母大寫的操作
public class StringDemo {    public static void main(String args[]) {        String str = "yootk";   // 定義字串        System.out.println(initcap(str));// 調用initcap()方法    }    /**     * 實現首字母大寫的操作     * @param temp 要轉換的字串資料     * @return 將首字母大寫後返回     */    public static String initcap(String temp) {        // 利用substring(0,1)取出字串的第一位而後將其大寫,再串連剩餘的字串        return temp.substring(0, 1).toUpperCase() + temp.substring(1);    }}程式執行結果:Yootk

Java基礎_0308: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.