天天看點

Java中String類方法整理

常用的轉換功能

class Function{
    public static void main(String[] args) {
        String s = "ABCDEF";
        //字元串 ----> 位元組數組
        byte[] bytes = s.getBytes();

        //字元串 ----> 字元數組
        char[] chars = s.toCharArray();

        //把各種基本資料類型或對象轉換成字元串
        String s1 = String.valueOf(3.14);

        //把字元串全部轉化為大寫或小寫
        String s2 = s.toUpperCase();
        String s3 = s.toLowerCase();

        //字元串拼接
        String s5 = s.concat("GH");
    }
}
           

常用方法示例1

public class StringMethod {
    public static void main(String[] args) {
        String s = "abcdef";
        //擷取目前字元串對象中,包含的字元個數
        System.out.println(s.length());

        //擷取字元串對象代表字元序列中,指定位置的字元
        System.out.println(s.charAt(1));

        //在目前字元串對象中查找指定的字元,找到就傳回字元首次出現位置,否則傳回-1
        s.indexOf("c");

        //查找目前字元串中,目标字元串首次出現的位置(如果包含),找不到傳回-1
        s.indexOf("ef");

        //查找首次出現的指定字元的位置,指定從某位置開始,(如果沒找到傳回-1)
        s.indexOf("f", 3);

        //查找首次出現的指定字元串的位置,從目前字元串對象的指定位置開始(如果沒找到傳回-1)
        s.indexOf("ef", 3);

        //傳回字元串:從指定位置開始(包含指定位置字元)到結束的那部分字元串
        s.substring(3);

        //傳回從start位置開始(包含),到end(不包含)結束字元串
        s.substring(2, 4);
    }
}
           

常用方法示例2

public class StringApi {
    public static void main(String[] args) {
        String s1 = "abcdE";
        String s2 = "abcde";
        String s3 = "abc";
        String s4 = "";
        String s5 = null;
        // 用來比較字元串的内容,注意區分大小寫
        // boolean equals(Object obj)
        System.out.println(s1.equals(s2));

        // 忽略字元串大小寫比較字元串内容,常見用于比較網址URL
        // boolean equalsIgnoreCase(String str)
        System.out.println(s1.equalsIgnoreCase(s2));

        // 判斷目前字元串對象是否包含,目标字元串的字元序列
        // boolean contains(String str)
        System.out.println(s1.contains(s3));

        // 判斷目前字元串對象,是否已目标字元串的字元序列開頭
        // boolean startsWith(String str)
        System.out.println(s1.startsWith(s3));

        // 判斷目前字元串,是否以目标字元串對象的字元序列結尾,常用于确定檔案字尾名格式
        // boolean endsWith(String str)
        System.out.println(s1.endsWith(s3));

        // 判斷一個字元串,是不是空字元串 "" null
        // boolean isEmpty()
        System.out.println(s4.isEmpty());

        // 擷取功能
        // 擷取目前字元串對象中,包含的字元個數
        // int length()
        System.out.println(s1.length());

        // 擷取字元串對象代表字元序列中,指定位置的字元
        // char charAt(int index)
        System.out.println(s1.charAt(3));

        // 在目前字元串對象中查找指定的字元,如果找到就傳回字元,首次出現的位置,如果沒找到傳回-1
        // 也可以填字元
        // int indexOf(int ch)
        System.out.println(s1.indexOf("d"));
        System.out.println(s1.indexOf(3));

        // 指定從目前字元串對象的指定位置開始,查找首次出現的指定字元的位置,(如果沒找到傳回-1)
        // 可以填入字元
        // int indexOf(int ch,int fromIndex)
        System.out.println(s1.indexOf(2,4));

        // 查找目前字元串中,目标字元串首次出現的位置(如果包含),找不到,傳回-1
        // 這裡的位置是指目标字元串的第一個字元,在目前字元串對象中的位置
        // int indexOf(String str)
        System.out.println(s1.indexOf("b"));

        // 指定,從目前字元串對象的指定位置開始,查找首次出現的指定字元串的位置(如果沒找到傳回-1)
        // 這裡的位置是指目标字元串的第一個字元,在目前字元串對象中的位置
        // int indexOf(String str,int fromIndex) ,
        System.out.println(s1.indexOf("b", 2));

        // 傳回字元串,該字元串隻包含目前字元串中,從指定位置開始(包含指定位置字元)到結束的那部分字元串
        // String substring(int start)
        System.out.println(s1.substring(2));

        // 傳回字元串,隻包含目前字元串中,從start位置開始(包含),到end(不包含)指定的位置的字元串
        // String substring(int start,int end)
        System.out.println(s1.substring(2, 4));
    }
}