国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

JAVA教學(xué)手冊 / Java String類

Java String類

字符串廣泛應(yīng)用在Java編程中,在Java中字符串屬于對象,Java提供了String類來創(chuàng)建和操作字符串。


創(chuàng)建字符串

創(chuàng)建字符串最簡單的方式如下:

String greeting = "Hello world!";

在代碼中遇到字符串常量時,這里的值是"Hello world!",編譯器會使用該值創(chuàng)建一個String對象。

和其它對象一樣,可以使用關(guān)鍵字和構(gòu)造方法來創(chuàng)建String對象。

String類有11種構(gòu)造方法,這些方法提供不同的參數(shù)來初始化字符串,比如提供一個字符數(shù)組參數(shù):

public class StringDemo{

   public static void main(String args[]){
      char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.'};
      String helloString = new String(helloArray);  
      System.out.println( helloString );
   }
}

以上實(shí)例編譯運(yùn)行結(jié)果如下:

hello.

注意:String類是不可改變的,所以你一旦創(chuàng)建了String對象,那它的值就無法改變了。 如果需要對字符串做很多修改,那么應(yīng)該選擇使用StringBuffer & StringBuilder 類。


字符串長度


用于獲取有關(guān)對象的信息的方法稱為訪問器方法。

String類的一個訪問器方法是length()方法,它返回字符串對象包含的字符數(shù)。

下面的代碼執(zhí)行后,len變量等于14:

public class StringDemo {
    public static void main(String args[]) {
        String site = "miracleart.cn";
        int len = site.length();
        System.out.println( "php中文網(wǎng)網(wǎng)址長度 : " + len );
   }
}

以上實(shí)例編譯運(yùn)行結(jié)果如下:

php中文網(wǎng)網(wǎng)址長度 : 14

連接字符串

String類提供了連接兩個字符串的方法:

string1.concat(string2);

返回string2連接string1的新字符串。也可以對字符串常量使用concat()方法,如:

"My name is ".concat("php");

更常用的是使用'+'操作符來連接字符串,如:

"Hello," + " world" + "!"

結(jié)果如下:

"Hello, world!"

下面是一個例子:

public class StringDemo {
    public static void main(String args[]) {     
        String string1 = "php中文網(wǎng)網(wǎng)址:";     
        System.out.println("1、" + string1 + "miracleart.cn");  
    }
}

以上實(shí)例編譯運(yùn)行結(jié)果如下:

1、php中文網(wǎng)網(wǎng)址:miracleart.cn

創(chuàng)建格式化字符串

我們知道輸出格式化數(shù)字可以使用printf()和format()方法。String類使用靜態(tài)方法format()返回一個String對象而不是PrintStream對象。

String類的靜態(tài)方法format()能用來創(chuàng)建可復(fù)用的格式化字符串,而不僅僅是用于一次打印輸出。如下所示:

System.out.printf("浮點(diǎn)型變量的的值為 " +
                  "%f, 整型變量的值為 " +
                  " %d, 字符串變量的值為 " +
                  "is %s", floatVar, intVar, stringVar);

你也可以這樣寫

String fs;
fs = String.format("浮點(diǎn)型變量的的值為 " +
                   "%f, 整型變量的值為 " +
                   " %d, 字符串變量的值為 " +
                   " %s", floatVar, intVar, stringVar);
System.out.println(fs);

String 方法

下面是String類支持的方法,更多詳細(xì),參看 Java String API 文檔:

                    SN(序號)                    方法描述
                    1char charAt(int index)
返回指定索引處的 char 值。
                    2int compareTo(Object o)
把這個字符串和另一個對象比較。
                    3int compareTo(String anotherString)
按字典順序比較兩個字符串。
                    4int compareToIgnoreCase(String str)
按字典順序比較兩個字符串,不考慮大小寫。
                    5String concat(String str)
將指定字符串連接到此字符串的結(jié)尾。
                    6boolean contentEquals(StringBuffer sb)
當(dāng)且僅當(dāng)字符串與指定的StringButter有相同順序的字符時候返回真。
                    7static String copyValueOf(char[] data)
返回指定數(shù)組中表示該字符序列的 String。
                    8static String copyValueOf(char[] data, int offset, int count)
返回指定數(shù)組中表示該字符序列的 String。
                    9boolean endsWith(String suffix)
測試此字符串是否以指定的后綴結(jié)束。
                    10boolean equals(Object anObject)
將此字符串與指定的對象比較。
                    11boolean equalsIgnoreCase(String anotherString)
將此 String 與另一個 String 比較,不考慮大小寫。
                    12byte[] getBytes()
 使用平臺的默認(rèn)字符集將此 String 編碼為 byte 序列,并將結(jié)果存儲到一個新的 byte 數(shù)組中。
                    13byte[] getBytes(String charsetName)
使用指定的字符集將此 String 編碼為 byte 序列,并將結(jié)果存儲到一個新的 byte 數(shù)組中。
                    14void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
將字符從此字符串復(fù)制到目標(biāo)字符數(shù)組。
                    15int hashCode()
返回此字符串的哈希碼。
                    16int indexOf(int ch)
返回指定字符在此字符串中第一次出現(xiàn)處的索引。
                    17int indexOf(int ch, int fromIndex)
返回在此字符串中第一次出現(xiàn)指定字符處的索引,從指定的索引開始搜索。
                    18int indexOf(String str)
 返回指定子字符串在此字符串中第一次出現(xiàn)處的索引。
                    19int indexOf(String str, int fromIndex)
返回指定子字符串在此字符串中第一次出現(xiàn)處的索引,從指定的索引開始。
                    20String intern()
 返回字符串對象的規(guī)范化表示形式。
                    21int lastIndexOf(int ch)
 返回指定字符在此字符串中最后一次出現(xiàn)處的索引。
                    22int lastIndexOf(int ch, int fromIndex)
返回指定字符在此字符串中最后一次出現(xiàn)處的索引,從指定的索引處開始進(jìn)行反向搜索。
                    23int lastIndexOf(String str)
返回指定子字符串在此字符串中最右邊出現(xiàn)處的索引。
                    24int lastIndexOf(String str, int fromIndex)
 返回指定子字符串在此字符串中最后一次出現(xiàn)處的索引,從指定的索引開始反向搜索。
                    25int length()
返回此字符串的長度。
                    26boolean matches(String regex)
告知此字符串是否匹配給定的正則表達(dá)式。
                    27boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
測試兩個字符串區(qū)域是否相等。
                    28boolean regionMatches(int toffset, String other, int ooffset, int len)
測試兩個字符串區(qū)域是否相等。
                    29String replace(char oldChar, char newChar)
返回一個新的字符串,它是通過用 newChar 替換此字符串中出現(xiàn)的所有 oldChar 得到的。
                    30String replaceAll(String regex, String replacement
使用給定的 replacement 替換此字符串所有匹配給定的正則表達(dá)式的子字符串。
                    31String replaceFirst(String regex, String replacement)
 使用給定的 replacement 替換此字符串匹配給定的正則表達(dá)式的第一個子字符串。
                    32String[] split(String regex)
根據(jù)給定正則表達(dá)式的匹配拆分此字符串。
                    33String[] split(String regex, int limit)
根據(jù)匹配給定的正則表達(dá)式來拆分此字符串。
                    34boolean startsWith(String prefix)
測試此字符串是否以指定的前綴開始。
                    35boolean startsWith(String prefix, int toffset)
測試此字符串從指定索引開始的子字符串是否以指定前綴開始。
                    36CharSequence subSequence(int beginIndex, int endIndex)
 返回一個新的字符序列,它是此序列的一個子序列。
                    37String substring(int beginIndex)
返回一個新的字符串,它是此字符串的一個子字符串。
                    38String substring(int beginIndex, int endIndex)
返回一個新字符串,它是此字符串的一個子字符串。
                    39char[] toCharArray()
將此字符串轉(zhuǎn)換為一個新的字符數(shù)組。
                    40String toLowerCase()
使用默認(rèn)語言環(huán)境的規(guī)則將此 String 中的所有字符都轉(zhuǎn)換為小寫。
                    41String toLowerCase(Locale locale)
 使用給定 Locale 的規(guī)則將此 String 中的所有字符都轉(zhuǎn)換為小寫。
                    42String toString()
 返回此對象本身(它已經(jīng)是一個字符串?。?。
                    43String toUpperCase()
使用默認(rèn)語言環(huán)境的規(guī)則將此 String 中的所有字符都轉(zhuǎn)換為大寫。
                    44String toUpperCase(Locale locale)
使用給定 Locale 的規(guī)則將此 String 中的所有字符都轉(zhuǎn)換為大寫。
                    45String trim()
返回字符串的副本,忽略前導(dǎo)空白和尾部空白。
                    46static String valueOf(primitive data type x)
返回給定data type類型x參數(shù)的字符串表示形式。