1. 程式人生 > 其它 >Java字串操作

Java字串操作

  1. 字串的基本操作

  2. 字串之間的比較

  3. 字串與其它資料型別之間的轉換

  4. 字元與字串的查詢

  5. 字串的擷取與拆分

  6. 字串的替換

1.字串的基本操作

 1. 獲取字串的長度 length()
 2. 獲取字串第i個位置的的字元   charAt()
 3. 獲取字串指定範圍的字元 getChars()
 4. 連線兩個字串  concat()
 5. 字串全部轉換成小寫   toLowerCase()
 6. 字串全部轉換成大寫   toUpperCase()
/**
 * @DESC  字串的基本操作
 *
 * @AUTHOR lh
 * @DATE 2022/5/10 8:57
 */
public
class StringBasicOpera { public static void main(String[] args) { // 先定義一個任意的字串 String str = "大鵬一日同風起,扶搖直上九萬里。"; ​ // 1. 獲取字串的長度 int length = str.length(); System.out.println("字串的長度為:" + length + "字元"); ​ // 2.獲取字串第i個位置的字元 char c = str.charAt(9); System.out.println(
"字串第9個位置的字元為:" + c); ​ // 3.獲取字串指定範圍的字元 char[] chars = new char[20]; str.getChars(0, 8, chars, 0); System.out.println(chars); char[] chars1 = new char[str.length()]; str.getChars(0, 16, chars1, 0); System.out.println(chars1); ​ // 4. 連線兩個字串 concat()
String str3 = "li"; String str4 = "BAI"; System.out.println(str3.concat(str4)); ​ // 5. 字串全部轉換成小寫/大寫 System.out.println(str3.toUpperCase()); System.out.println(str4.toLowerCase()); } }

2.字串之間的比較

兩種比較情況:

  • 比較兩個字串的大小,比較的依據是字元的Unicode編碼值。

    compareTo()

    conpareToIgnoreCase()

  • 比較兩個字串是否相等,返回true or false。

    equals()

    equalsIgnoreCase()

/**
 * @DESC 字串比較
 * @AUTHOR lh
 * @DATE 2022/5/10 9:17
 */
public class StringCompare {
​
    public static void main(String[] args) {
        String str1 = "apple";
        String str2 = "Apple";
​
        // 比較兩個字串大小
        System.out.println("====不忽略大小寫====");
        if (str1.compareTo(str2) > 0) {
            System.out.println(str1 + ">" + str2);
        } else if (str1.compareTo(str2) == 0) {
            System.out.println(str1 + "=" + str2);
        } else {
            System.out.println(str1 + "<" + str2);
        }
        System.out.println("====不忽略大小寫====");
​
        System.out.println("====忽略大小寫====");
        if (str1.compareToIgnoreCase(str2) > 0) {
            System.out.println(str1 + ">" + str2);
        } else if (str1.compareToIgnoreCase(str2) == 0) {
            System.out.println(str1 + "=" + str2);
        } else {
            System.out.println(str1 + "<" + str2);
        }
        System.out.println("====忽略大小寫====");
​
        System.out.println("====不忽略大小寫比較====");
        if (str1.equals(str2)) {
            System.out.println(str1 + "=" + str2);
        } else {
            System.out.println(str1 + "!=" +str2);
        }
        System.out.println("====不忽略大小寫比較====");
​
        System.out.println("====忽略大小寫比較====");
        if (str1.equalsIgnoreCase(str2)) {
            System.out.println(str1 + "=" + str2);
        } else {
            System.out.println(str1 + "!=" +str2);
        }
        System.out.println("====忽略大小寫比較====");
    }
​
}

3.字串與其它資料型別之間的轉換

兩種情況:

  • 將字串型別轉換為其他資料型別

  • 其他資料型別轉換成字串型別

/**
 * @DESC 字串型別轉換
 * @AUTHOR lh
 * @DATE 2022/5/10 9:36
 */
public class StringConversion {
    public static void main(String[] args) {
        System.out.println("====字串型別轉換成其它資料型別====");
        boolean b = Boolean.parseBoolean("true");
        System.out.println(b);
        int i = Integer.parseInt("1024");
        System.out.println(i);
        long l = Long.parseLong("1024");
        System.out.println(l);
        float f = Float.parseFloat("1.024");
        System.out.println(f);
        double d = Double.parseDouble("1.024");
        System.out.println(d);
        byte b1 = Byte.parseByte("10");
        System.out.println(b1);
        System.out.println("====字串型別轉換成其它資料型別====");
​
​
        System.out.println("====其它資料型別轉換成字串型別====");
        String valueOfb = String.valueOf(b);
        System.out.println(valueOfb);
        String valueOfs = String.valueOf(i);
        System.out.println(valueOfs);
        String valueOff = String.valueOf(f);
        System.out.println(valueOff);
        String valueOfd = String.valueOf(d);
        System.out.println(valueOfd);
        String valueOfb1 = String.valueOf(b1);
        System.out.println(valueOfb1);
        System.out.println("====其它資料型別轉換成字串型別====");
    }
}

擴充套件:str.toString()方法同樣能夠轉換字串,為什麼用String.valueOf()

  • String.valueOf()可以接受Object物件,但是toString方法必須是實現了toString物件的方法才能使用。

  • toString方法容易引起空指標異常

    System.out.println(obj.toString());

    如果上面的obj物件是空的,那麼就會引起空指標異常。

  • toString方法容易引起型別轉換異常

    Integer.valueOf(obj.toString());

    如果obj傳過來的不是純數字,在進行型別轉換時就會報錯。

因此,在進行字串型別轉換時,儘量使用String.valueOf(),扔進去什麼都可以轉換成string字串,即便是null也可以。

4.字元與字串的查詢

兩種情況:

  • 查詢字串

  • 查詢子串

查詢第一次出現的位置,查詢最後一次出現的位置。

指定索引位置,從哪裡開始查詢。

indexOf()

lastIndexOf()

/**
 * @DESC 字串查詢
 * @AUTHOR lh
 * @DATE 2022/5/10 10:07
 */
public class StringSearch {
    public static void main(String[] args) {
        String str = "i love you.i really love you.";
​
        int firstV = str.indexOf("v");
        System.out.println("第一個v所在的位置為:" + firstV);
        int fromV = str.indexOf("v", 6);
        System.out.println("索引6後面的第一個v所在的位置為:" + fromV);
​
        int lastV = str.lastIndexOf("v");
        System.out.println("最後一個v所在的位置為:" + lastV);
        int lastfromV = str.lastIndexOf("v", 25);
        System.out.println("索引25前面的第一個v所在的位置為:" + lastfromV);
​
        int firstLove = str.indexOf("love");
        System.out.println("第一個love子串所在的位置為:" + firstLove);
        int fromLove = str.indexOf("love", 8);
        System.out.println("索引8後面的第一個love子串所在的位置為:" + fromLove);
​
        int lastLove = str.lastIndexOf("love");
        System.out.println("最後一個love子串所在的位置為:" + lastLove);
        int lastFromLove = str.lastIndexOf("love", 15);
        System.out.println("索引15前面的第一個love子串所在的位置為:" + lastFromLove);
    }
}

5.字串的擷取與拆分

  • substring()截取出一個長字串中的一個子字串

  • 按照正則表示式要求全部拆分儲存到一個字串陣列

/**
 * @DESC 字串的擷取與拆分
 * @AUTHOR lh
 * @DATE 2022/5/10 10:24
 */
public class StringCutAndSplit {
    public static void main(String[] args) {
        String str = "i love you i always love you";
​
        // 第一個空格所在的位置
        int pre = str.indexOf(" ");
        // 最後一個空格所在的位置
        int last = str.lastIndexOf(" ");
​
        String firstWord = str.substring(0, pre);
        String lastWord = str.substring(last + 1, str.length());
        System.out.println("第一個單詞為:" + firstWord);
        System.out.println("最後一個單詞為:" + lastWord);
​
        String[] splitStr = str.split(" ");
        System.out.println("拆分的字串單詞為:");
        for (String s : splitStr) {
            System.out.println(s + "\t");
        }
    }
}

6.字串的替換

/**
 * @DESC 字串替換
 * @AUTHOR lh
 * @DATE 2022/5/10 10:47
 */
public class StringReplace {
    public static void main(String[] args) {
        String str = "apple";
​
        String replaceFirst = str.replaceFirst("p", "z");
        String replaceAll = str.replaceAll("p", "z");
​
        System.out.println(replaceFirst);
        System.out.println(replaceAll);
​
    }
}