1. 程式人生 > >菜雞的Java課筆記 第十四 String 類常用方法

菜雞的Java課筆記 第十四 String 類常用方法

/*String 類常用方法
    將所有String類的常用方法全部記下來,包括方法名稱,引數作用以及型別
    一個成熟的程式語言,除了它的語法非常完善之外,那麼也需要提供有大量的開發類庫
    而需要知道的java的類庫除了本身提供的JDKDoc之外還會有大量的
    本次操作主要是以文件的查詢為主,文件沒有中文的,只有英文或者是日文。
    一般來講每一份文件的組成都會包含有如下幾個部分:
        1.類的定義以及實現的父類,父介面,子類,子介面等
        2.類的一些使用的簡單的簡單說明
        3.成員摘要說明
        4.構造說明
        5.普通方法說明
        6.具體的成員,構造,普通方法依次的完整解釋
    理論上以後所編寫的開發程式碼大家都是需要動態查詢文件得到的,但是像一般比較常用的類或者是方法就需要大家記住了
    在本次講解完成之後,要求記住所講解方法的作用,返回值的意義,引數的型別,引數的意義
*/
// Deprecated  看到這個單詞表示這個方法不用了
/*    一,字元與字串
        很多的程式語言都會強調利用字元陣列來描述字串,實際上在java裡面也存在有類似的概念
        在String類中也提供有一系列與字元操作有關的方法

方法名稱 型別 說明
public String(char[]value) 普通 將全部的字元陣列作為String的內容
public String(char[]value,int offset,int count) 構造 將部分的字元陣列變為字串,offset:開始點,count:個數
public char charAt(int index) 普通 返回指定索引位置上的字元
public char[] toCharArray()
普通 將字串以字元陣列的形式返回



        範例:驗證charAt()方法

public class commonMethod{
    public static void main(String args[]){
        String str = "hello";char c = str.charAt(0);  // 如果是 1 返回的就是 e
        System.out.println(c);  // h
    }
}


            在程式中字串索引的都是從0開始的,資料庫是從1開始的
            而後最為關鍵的是實現字串與字元陣列間的互相轉換
        範例:將字串變為字元陣列

public class commonMethod{
    public static void main(String args[]){
        String str = "helloworld";char[] dsta = str.toCharArray(); // 將字串變為字元陣列
        for(int x = 0:x<dsta.length;x++){
            System.out.println(dsta[x]+"."); // h.e.l.l.o.w.o.r.l.d
        }
    }
}


            當字串變為字元陣列之後就可以針對與裡面的每一個字元進行操作
        範例:將字串中的小寫字母變為大寫字母
            小寫字母的編碼>大寫字母的編碼,差了32,也就是說每個字元都減去32即可

public class commonMethod{
    public static void main(String args[]){
        String str = "helloworld";char[] dsta = str.toCharArray(); // 將字串變為字元陣列
        for(int x = 0:x<dsta.length;x++){
            dsta[x]-=32; // 編碼減去32

        }
        System.out.println(new String(dsta)); 
    // new String 可以把 字元陣列變為字串 // 結果:HELLOWORLD System.out,println(new String (dsta,5,5));
    // 第5個索引之後取5個長度的內容 // 結果:WORLD } }

            下面可以利用此操作功能判斷某一個字串是否全部由數字所組成
                將字串變為字元陣列,這樣做的目的可以進行每個字元的依次判斷
                判斷每一個字元的範圍是否是數字:“0”~“9”之間
        範例:現實判斷
            為了實現判斷方便可以單獨定義有一個isNumber()方法,這個方法返回boolean型別,但是在java裡面有一種命名規範
            如果方法返回的是boolean型別,建議以“isXxx()”命名

public class commonMethod{
    public static void main(String args[]){
        String str = "helloworld";
        System.out.println(isNumbar(str));
    }
    public static boolean isNumbar(String temp){
        char dsta[] = temp.toCharArray();  // 將字串變為字元陣列
        for(int x = 0:x<dsta,length;x++){
            if(dsta[x]<'0'||dsta[x]>'9'){  // 不是數字
                return false;
            }
        }
        return true;
    }
}// 結果:false

*/

/*    位元組與字串   Bytes and strings
        除了字元可以與字串進行互相轉換之外,位元組也可以進行轉換,但是這樣的轉換往往會出現在實際的開發環節之中

方法名稱 型別 說明
public String(byte[] bytes) 普通 將全部位元組陣列變為字串
public String(byte[]byte,int offset,int count) 構造 將部分位元組陣列變為字串
public byte[]getBytes()  普通 將字串變為位元組陣列
public byte[]getBytes(String charsetName) 普通 編碼轉換
  throwsUnsupporledEncodingException


            首先對於byte 一定要清楚,雖然它的範圍要比char小,但是byte還是可以明確的描述出字母的範圍的
        範例:利用位元組陣列實現小寫字母變大寫字母的操作

public class bytesAndStrings{
    public static void main(String args[]){
        String str = "helloworld";byte dsta[] = str.getBytes();  // 將字串變為位元組陣列
        for(int x = 0:x<dsta.length:x++){
            dsta[x]-=32;  // 改變編碼
        }
        System.out.println(new String(dsta));  // HELLOWORLD
    }
}


            以上的這種程式碼只能夠說針對於位元組的使用入了個門,而實際中位元組的時候需要結合IO,結合網路一起分析才能夠進行
*/

/*    string comparison    字串比較
        現在至少應該知道了字串比較有equals()方法
        範例:觀察equals()的問題

public class stringComparison{
    public static void main(String args[]){
        String strA = "hello";
        String strB = "HELLO";
        System.out.println(strA.equals(strB));  // false
    }
}


            但是此方法本身是屬於區分大小寫的比較,在此在java裡面對於字串的比較實際上還有其他方法

方法名稱 型別 說明
 public boolean equals(String anObject) 普通 區分大小寫的字串比較
public boolean equalsIgnoreCase(String anobjectString) 普通 不區分大小寫的字串比較
public int compareTo(String anobjectString) 普通 比較字串大小
public int compareToIgnoreCase(String str) 普通 不區分大小寫比較字串大小



            不區分大小寫的字串比較

public class stringComparison{
    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
    }
}

            驗證碼操作中可真的不分大寫或者是小寫字母
        在進行字串相等的比較操作之中,最為關鍵的一個方法是:compareTo()
        這個方法本身返回一個int型資料,而這個int型資料有三種結果:大於(>0),小於(<0),等於(=0)
        範例:比較字串大小(A 比較 a)

public class stringComparison{
    public static void main(String args[]){
        String strA = "A";
        String strB = "a";
        System.out.println(strA.compareTo(strB));  // -32
        System.out.println(strA.compareToIgnoreCase(strB));  // 0
    }
}

            在compareTo()方法之中要進行比較是依據編碼進行相減得來的
            如果是System.out.println(strA.compareToIgnoreCase(strB)):  // 0  那就是忽略大小寫
            所以利用compareTo()返回值範圍進行判斷就可以區分大小關係了

public class stringComparison{
    public static void main(String args[]){
        String strA = "A";
        String strB = "a";
        System.out.println(strA.compareTo(strB));  // -32
        System.out.println(strA.compareToIgnoreCase(strB));  // 0
        if(strA.compareTo(strB)==0){
            System.out.println("兩個字串的內容相同");  //  
        }
    }
}


*/

/*    字串查詢
        從一個完整字串之中查詢一些字串,而可以實現這種字串查詢功能的方法有如下幾個

 方法名稱 型別 說明
public boolean contains(String s) 普通 判斷某一個字串是否存在
public int indexOf(String str) 普通 取得某一個子字串的索引位置,找不到返回-1
public int indexOf(String str,int fromIndex) 普通 從指定索引位置開始檢索子字串位置,找不到返回-1
public int lastIndexOf(String str) 普通 從後向前查詢指定子字串的位置,找不到返回-1
public int lastIndexOf(String str,int fromIndex) 普通 從指定位置由後向前查詢子字串的位置,找不到返回-1
public boolean startsWith(String prefix) 普通 判斷是否以某個字串開頭
public boolean startsWith(String prefix,int toffset) 普通 從指定位置判斷是否以某個字串開頭
public boolean endsWith(String suffix) 普通 是否以指定的字串結尾


            如果要查詢中間的內容,那麼現在基本上都是使用contains()方法了
        範例:使用contains()方法

public class stringLookup{
    public static void main(String args[]){
        String str = "hello";  // 0:h,1:e,2:l,3:l,4:o
        if(str.contains("l")){  // 此方法直接返回true 或者是false
            System.out.println("查詢到!!!");
        }
    }
}

            contains()方法雖然現在用的比較多,但是其是在JDK1.5後才提供的。
            而在JDK1.5之前那麼只能夠通過indexOf()方法實現
        範例:利用indexOf()方法判斷

public class stringLookup{
    public static void main(String args[]){
        String str = "hello";  // 0:h,1:e,2:l,3:l,4:o
        System.out.println(str.indexOf("l"));  // 2
        if(str.indexOf("l")!=-1){ // 現在表示索引查詢到了
            System.out.println("內容已經查詢到!!!");
        }
    }
}

            預設情況下intdexOf()都是從第一個字母開始查詢。那麼也可以利用其的過載方法從指定索引位置查詢,通過原始碼可以發現 contains() 方法只是對 indexOf() 方法的一種包裝
        範例:其他方法

public class stringLookup{
    public static void main(String args[]){
        String str = "hello";  // 0:h,1:e,2:l,3:l,4:o
        System.out.println(str.indexOf("l",2));  // 如果是從3開始 返回的就是3  // 2
        System.out.println(str.lastIndexOf("l")); // 3
    }
}

            在字串查詢操作裡面還可以判斷開頭或者結尾
        範例:判斷開頭或者結尾

public class stringLookup{
    public static void main(String args[]){
        String str = "***@@@hello###";
            System.out.println(str.startsWith("**"));  // true
            System.out.println(str.startsWith("@@",2)); // true
            System.out.println(str.endsWith("##"));  // true
    }
}

            這幾個查詢現在出現最多的就是contains(),startsWith(),endsWith()
以後學習到後期設計的時候,對字串的處理(格式定義),這些方法極為重要

*/

/*    substr 字串的擷取
        可以通過一個完整的字串擷取裡面的子字串,那麼這個時候使用方法如下:

方法名稱 型別 說明
public String substring(int beginIndex) 普通 從指定索引位置擷取到結尾
public String substring(int beginIndex,int endIndex) 普通 擷取指定索引範圍的內容


        範例:觀察擷取操作

public class substr{
    public static void main(String args[]){
        String str = "helloworldsubstr";
        System.out.println(str.substring(5));  // 擷取 結果  worldsubstr
        System.out.println(str.substring(5,10));  // world
        System.out.println(str.substring(0,5));  // hello
    }
}

            千萬不要忽略了一點,實際的工作之中,這種擷取操作很常用
*/

/*    stringReplacement 字串替換
        將一個指定的字串替換為其他內容,就屬於替換功能,在String類中有如下的方法支援替換操作

方法名稱 型別 說明
 public String replaceAll(String regex,String replacement) 普通 字串的全部替換
 public String replaceFirst(String regex,String replacement) 普通 替換第一個內容


        範例:觀察替換操作

public class stringReplacement{
    public static void main(String args[]){
        String str = "helloworldsubstr";
        System.out.println(str.replaceAll("l","_"));  // he__owor_dsubstr
        System.out.println(str.replaceFirst("l","_"));  // he_loworldsubstr
    }
}

            實際上利用替換的功能可以消除掉資料中的空格

public class stringReplacement{
    public static void main(String args[]){
        String str = "h e l l o w o r l d substr";
        System.out.println(str.replaceAll(" ",""));  // helloworldsubstr
    }
}

對於替換的操作後期還要結合正則表示式來進行深入講解
*/

/*    String resolution    字串拆分
        可以將一個完整的字串根據指定的內容進行拆分,拆分後的結果就是多個字串,也就是一個字串的物件陣列
        可以使用的操作方法有如下幾個:

方法名稱 型別 說明
public String split(String regex) 普通 全部拆分,陣列長度為拆分後的子字串個數
public String split(String regex,int limit) 普通  部分拆分


        範例:觀察Split()方法的使用

public class stringResolution{
    public static void main(String args[]){
        String str = "hello world mldn nihao";
        String result[] = str.split(" ");  // 全部拆分
        for(int x = 0:x<result.length:x++){
            System.out.println(result[x]);
        }
    }
}
/*
結果:
hello
world
mldn
nihao
*/


            現在是按照空格全部進行了拆分
        範例:拆分部分內容

public class stringResolution{
    public static void main(String args[]){
        String str = "hello world mldn nihao";
        String result[] = str.split(" ",2);  // 全部拆分
        for(int x = 0:x<result.length:x++){
            System.out.println(result[x]);
        }
    }
}
/*
結果:
hello
world mldn nihao
*/

            現在如果給你一個ip地址,那麼一定是按照“.”拆
        範例:拆分 IP

public class stringResolution{
    public static void main(String args[]){
        String str = "192.168.1.1";
        String result[] = str.split(".");  // 全部拆分
        for(int x = 0:x<result.length:x++){
            System.out.println(result[x]);
        }
    }
}
// 結果:(沒有結果)

            原因:這個“.”不能夠直接使用,因為它屬於正則表示式的定義範疇
                    所以日後如果發現不能夠拆分的資料必須進行轉義,使用“\\”

public class stringResolution{
    public static void main(String args[]){
        String str = "192.168.1.1";
        String result[] = str.split("\\.");  // 全部拆分
        for(int x = 0:x<result.length:x++){
            System.out.println(result[x]);
        }
    }
}
/*
結果:
192
168
1
1
*/


            在字串的組成之中,也可能包含有“|”問題

public class stringResolution{
    public static void main(String args[]){
        String str = "192|168|1|1";
        String result[] = str.split("|");  // 全部拆分
        for(int x = 0:x<result.length:x++){
            System.out.println(result[x]);
        }
    }
}
/*
結果:
1
9
2
|
1
6
8
|
1
|
1
*/

---

public class stringResolution{
    public static void main(String args[]){
        String str = "192|168|1|1";
        String result[] = str.split("\\|");  // 全部拆分
        for(int x = 0:x<result.length:x++){
            System.out.println(result[x]);
        }
    }
}
/*
結果:
192
168
1
1
*/


            發現 regex 這個單詞 這個就是正則的定義範疇,就需要轉義
*/

/*    其他操作方法
        以上是一些可以進行歸類的操作方法,但是有一些操作方法在日後的課程裡面還會有詳細的講解
        而還有一些小的操作方法

方法名稱 型別 說明
public int length() 普通 取得字串的長度
public boolean isEmpty() 普通 判斷是否為空字串(不是null)
public String toLowerCase()    普通 轉為小寫
public String toUpperCase() 普通 轉為大寫
public String trim() 普通 去掉左右空格,但是不去掉中間內容
public String concat(String str) 普通 字串連線,與“+”一樣
public String intern() 普通 手工入池


        範例:計算字串的長度

public class commonMethod{
    public static void main(String args[]){
        String str = "上課睡覺";
        System.out.println(str,length());  // 方法有括號
    }
}


            因為JAVA使用了Unicode編碼,所以中文與英文的長度相同,這一點極大的方便了開發者,在陣列定義中存在有一個 陣列名稱.length 它可以獲取陣列的長度
            String 類中使用的是length()方法,而陣列中使用的是“length”屬性           
        範例:判斷是否為空字串

public class commonMethod{
    public static void main(String args[]){
        String str = "上課睡覺";
        System.out.println(str,isEmpty());  // 結果:false
        System.out.println("",isEmpty());  // 結果:true
    }
} // 結果:false

       
        範例:轉大,小寫操作

public class commonMethod{
    public static void main(String args[]){
        String str = "Hello World @#&*()(";
        System.out.println(str,toLowerCase());  // 結果:hello world @#&*()(
        System.out.println(str,toUpperCase());  // 結果:HELLO WORLD @#&*()(
    }
}


            所有字母的內容都發生了轉變,但是非字母的內容不會發生改變
        範例:觀察trim()

public class commonMethod{
    public static void main(String args[]){
        String str = " hello world ";
        System.out.println("內容:"+str);  // 結果: hello world
        System.out.println("內容:"+str.trim());   // 結果:hello world
    }
}

           
        範例:觀察 concat()

public class commonMethod{
    public static void main(String args[]){
        String str = " hello ".concat("world ");
        System.out.println("內容:"+str);  // 結果: hello world
    }
}


            基本上開發中不會使用 concat() 實現字串的連線,都會使用“+”描述
        非常遺憾的是在整個String類設計的過程之中並沒有提供有類似於資料庫中的 initcap() 函式的功能
    雖然String類提供有大量的操作方法支援,但是有一個功能其並沒有提供:首字母大寫
        範例:實現一個首字母大寫的操作形式

public class commonMethod{
    public static void main(String args[]){
        String att = "name";
        System.out.println(initcap(att));  // 結果: Name
    }
    public static String initcap(String str){
        if(str == null||str,length()==0){
            return str;
        }
        return str.substring(0,1).toUpperCase()+str.substring(1);
    }
}

            進行一些原理分析的時候,此類程式碼就需要使用到了
*/