java基礎總結之String性質和常用方法

來源:互聯網
上載者:User

標籤:package   建立   修飾符   指定   大小   資料   大寫   copy   data   

package cn.wen.basic.test;import java.io.UnsupportedEncodingException;import java.util.Arrays;import org.junit.Test;/* * 面試題 string和stringBuffer的區別 * 系統複習之string * string是一個常量  一旦輸入不能修改,重新賦值是兩個對象 * 源碼中可以看到 string被final修飾符修飾 為常量 * String繼承Serializable(可序列化的)介面和Comparable(比較類)和charSequence(序列化字串)類 * 源碼對該類的定義為該值用於字元儲存 * api中說String建立的值不能變。字串緩衝區可以變(StringBuffer)。String支援共用 */public class testString {/* * string method 往後要學會記方法名 * string的共用性 * char類型的支符數組可以轉化為string類型的字串  * String源碼中有這個構造方法來實現char數群組轉換為String *  public String(char value[]) {        this.value = Arrays.copyOf(value, value.length);    } */@Testpublic void  testStringCharShare(){String str="abc";System.out.println(str);//相當於char[] data={‘a‘,‘b‘,‘c‘};String str1=new String(data);System.out.println(str1);}     @Test     public void testStringConstructor(){     String str0=new String();//初始化新建立的String對象,使其表示Null 字元序列     byte[] bytes=new byte[1024];     String str1=new String(bytes);//通過使用平台預設字元集解碼指定的位元組數組來構造新的string;     str1="adfs";     System.out.println(str1);     try {String str2=new String(bytes,"UTF-8");//設定字元集解碼指定的位元組數組來構造新的string//類似方法String str3=new String(bytes,1,bytes.length-1);//通過使用平台預設字元集解碼指定的位元組子列陣來構造新的string。String str4=new String(bytes,0,bytes.length-1,"UTF-8");//bytes位元組數組 0指定截取的位元組的開始位置,length指定截取位元組的最後位置。utf-8指定解碼集char[] data1={‘a‘,‘b‘,‘c‘,‘d‘,‘e‘,‘f‘};String str5=new String(data1,1,4);System.out.println(str5);} catch (UnsupportedEncodingException e) {//如果不支援該類型的編碼或者編碼格式輸入錯誤拋出異常// TODO Auto-generated catch blocke.printStackTrace();}               }     @Test     public void testStringMethod(){     String str1="abcdefghijklmnopqrstuvwxyz";     char d=str1.charAt(3);//擷取string字串中的支符 返回char類型     System.out.println(d);     int a=str1.codePointAt(0);//擷取第index個字元的acci碼值     System.out.println(a);     int c=str1.codePointBefore(4);//擷取第index個字元的前一個字元的acci碼值     System.out.println(c);     int acci=str1.codePointCount(1, 5);//擷取指定範圍的unicode代碼點數     System.out.println(acci);     String str2="c";     int comp=str1.compareTo(str2);//按字典序比較兩個字串的大小。      System.out.println(comp);     System.out.println("-------------------------");     String[] str3=str1.split("d");//分割字串 返回d以前和d以後的字串  不包含d     for(int i=0;i<str3.length;i++){                System.out.println(str3[i]);     }     System.out.println("--------------------------");     String strSon=str1.substring(1, 5);//截取字串 1代表起始位置,5代表結束位置     System.out.println(strSon);     String strSon1=str1.substring(3);//截取字串,從3開始     System.out.println(strSon1);     CharSequence cs=str1.subSequence(1, 4);//返回一個字元序列     System.out.println(cs);     String strUpper=str1.toUpperCase();//小寫變大寫 //toLowerCase 大寫變小寫     System.out.println(strUpper);     }
}

  String類在Java中是非常常用的類,String提供了很多非常好的方法協助程式員進行字串的操作,這在jee中非常有用,因為前台傳過來的資料往往是字串或者json數組的形式。

java基礎總結之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.