Java學習lesson 11

來源:互聯網
上載者:User

標籤:string   buffer   character   

String類中的其他功能:

  替換功能:

 * public String replace(char oldChar,char newChar):將字串中某一個字元用新的字元替換

 * public String replace(String oldStr,String newStr):將字串中某一個子字串用新 的字元                                     串去替代

 * public String trim():去除字串兩端空格:

 

 * public int compareTo(String anotherString)  是Comparable介面中的方法(該介面可以實現一                                  個自然排序)

 * Comparator介面可以比較子排序

package string;public class StringDemo1 {public static void main(String[] args){//定義字串String str="helloword";System.out.println(str);//將原字串中的字元“l”用“k”替換,String str1=str.replace("l", "k");System.out.println(str1);//將原字串中的字串“hello”用“hi——”替換String str2=str.replace("hello", "hi——");System.out.println(str2);System.out.println("----------------");String str3="  helloword  ";System.out.println(str3);//返回字串的副本,忽略前置空白和尾部空白String str4=str3.trim();System.out.println(str4);System.out.println("----------------");String str5 ="helloword" ;String str6="abc";System.out.println("compareTo():"+str5.compareTo(str));System.out.println("compareTo():"+str6.compareTo(str));}}


650) this.width=650;" title="String2.png" alt="27f1921f89f20f251f23ddcb185610fa.png-wh_" src="https://s5.51cto.com/oss/201711/02/27f1921f89f20f251f23ddcb185610fa.png-wh_500x0-wm_3-wmp_4-s_3341893237.png" />

 * int indexOf(String str):返回指定字串在此字串中的一次出現的索引

 * int indexOf(Stirng  str,int fromIndex):返回次字串中第一次出現指定字串出的索引,從                              指定索引開始

 * String substring(int start):從指定位置開始截字串,預設到最末尾

 * String substring(int start,int end):從指定位置開始截取到指定位置結束

package string;import java.util.Scanner;public class StringDemo5 {public static void main(String[] args){String str="java";Scanner sc=new Scanner(System.in);System.out.println("請你輸入一個字串:");String str1=sc.nextLine();System.out.println("你輸入的字串是:"+str1);System.out.println("----------------");//從頭開始返回java第一次出現的首地址System.out.println("str1.indexOf(str):"+str1.indexOf(str));//ugfuyjavaojgojjavagfdug//從指定索引開始System.out.println("str1.indexOf(str,7 ):"+str1.indexOf(str,7 ));System.out.println("-------------------");//從指定位置截取字串,預設到末尾結束System.out.println("str1.substring(3):"+str1.substring(3));//從指定位置截取字串,到指定位置結束System.out.println("str1.substring(3,7):"+str1.substring(3,7));}}


650) this.width=650;" title="String3.png" alt="6314e9f6eee39f572aea1e840cb4c2a9.png-wh_" src="https://s4.51cto.com/oss/201711/02/6314e9f6eee39f572aea1e840cb4c2a9.png-wh_500x0-wm_3-wmp_4-s_289388021.png" />


* StringBuffer類

    安全執行緒的可變字元序列,類似於String的字串緩衝區

   *開發中線程不安全的話,可能會出現死結的現象!安全執行緒和執行效率是相對的

   * 面試題

     *StringBuffer與String的區別?

     *StringBuffer會構造一個字串緩衝區,從記憶體角度考慮使用StringBuffer比較多

      String是一個普通的字串類,從記憶體角度耗費空間!

      在單線程程式中使用StringBulider(線程不安全)代替StringBuffer

   *StringBuffer中的常用方法

      * 構造方法:

             *public StringBuffer(){ }

              構造一個不帶字元的字串緩衝區,該字串緩衝區的初始容量為16個字元

             *public StringBuffer(int capacity)

              構造一個不帶字元,但具有指定初始容量的字串緩衝區

             *public StringBuffer(String str)

             構造一個字串緩衝區,並將其內容初始化為指定的字串內容。該字串的初               始容量為 16 加上字串參數的長度。

 

 

             * public int length();擷取字串長度

             * public int capacity():擷取當前字串緩衝區的容量 

    *StringBuffer的添加方法:

             * public StringBuffer append( );向緩衝區追加資料,返回字串緩衝區本身

             * public StringBUffer insert(int offset,String str):在指定位置添加一個               字串,返回字串緩衝區本身

     *StringBuffer的刪除方法:

             * public StringBuffer deleteCharAt(int start):刪除指定位置的字元,返回                                             字串緩衝區本身

             * public StringBuffer deleteCharAt(int start,int end):刪除從指定位置開                                            始到指定位置結束的字元返回                                            字串緩衝區本身


 

package stringbuffer;public class StringBufferDemo {public static void main(String[] args ){//StringBuffer不可以像String一樣直接賦值,可以建立對象利用構造方法賦值//也可以通過無參構造,調用append();方法進行添加StringBuffer sb=new StringBuffer();sb.append("hello");sb.append("world");sb.append("-----javase");System.out.println("添加初始的字串:"+sb);sb.insert(5, "-----");System.out.println("用insert()添加:"+sb);System.out.println("----------------");sb.deleteCharAt(3);System.out.println("刪除一個字元後的字串:"+sb);System.out.println("----------------");//當前索引所對應的字元一發生了改變sb.delete(1, 3);System.out.println("刪除一個小字串後:"+sb);System.out.println("----------------");}}


650) this.width=650;" src="https://s2.51cto.com/oss/201711/03/9672ad13b84d5b0fa0cbcff2f789b9ec.png-wh_500x0-wm_3-wmp_4-s_3940078985.png" title="append.png" alt="9672ad13b84d5b0fa0cbcff2f789b9ec.png-wh_" /> 


     *StringBuffer和String之間的轉換

     *StringBuffer的反轉功能:

      public StringBuffer reserve();將字串順序直接翻轉 

        

package stringbuffer;public class StringBufferDemo1 {public static void main(String[] args) {    String str="hello";    //String----->StringBuffer的轉換    System.out.println("String----->StringBuffer的轉換");    //建立StringBuffer的對象指向這個字串    StringBuffer sb=new StringBuffer(str);    System.out.println("建立StringBuffer的對象指向這個字串:"+sb);        //使用無參構造,調用append()方法    StringBuffer sb1=new StringBuffer();    sb1.append(str);    System.out.println("使用無參構造,調用append()方法:"+sb1);    System.out.println("----------------");        //StringBuffer----->String的轉換    System.out.println("StringBuffer----->String的轉換");    //建立緩衝區對象    StringBuffer sb2=new StringBuffer("world");    //String有一種構造方法String(StringBuffer str)    String sb3=new String(sb2);    System.out.println("構造方法String(StringBuffer str):"+sb3);    //toString()方法    String sb4=sb2.toString();    System.out.println("toString()方法:"+sb4);    System.out.println("----------------");        //使用StringBuffer的翻轉功能將字串反轉“reverse()”    System.out.println("String類型“hello”使用reverse()翻轉後:"+sb2.reverse().toString());        }}

 

650) this.width=650;" src="https://s2.51cto.com/oss/201711/03/bedb92d12cf1648edb42887697b54fbd.png-wh_500x0-wm_3-wmp_4-s_286552101.png" title="reverse.png" alt="bedb92d12cf1648edb42887697b54fbd.png-wh_" /> 

     *StringBufferd的替換功能:

      *public StringBuffer replace(int start,int end,String str):從指定位置開始到指定位                                              置用str替換

     *StringBuffer的截取功能:

      *public StringBuffer substring(int start):從指定位置開始到末尾結束,返回一個新的字                                   符串

     *public StringBuffer substring(int start,int end):從指定位置開始到指定位置結束,返                                       回一個新的字串


 

package stringbuffer;public class StringBufferDemo2 {public static void main(String[] args){StringBuffer sb=new StringBuffer();sb.append("helloworld");System.out.println("初始字串:"+sb);System.out.println("替換後:"+sb.replace(0, 5, "hi-"));System.out.println("------------------");//StringBuffer的截取功能//從指定位置開始截取,預設到結尾結束System.out.println("從的指定位置往後截取:"+sb.substring(5));    System.out.println("從指定位置開始到指定位置結束:"+sb.substring(3, 6));}}


650) this.width=650;" src="https://s1.51cto.com/oss/201711/03/eca29d24d9bad03094077e21203f77a6.png-wh_500x0-wm_3-wmp_4-s_1028603263.png" title="replace.png" alt="eca29d24d9bad03094077e21203f77a6.png-wh_" />


    問題:

    *String、StringBuffer、StringBuleder的區別

    String: 一個不可變的字元序列

    StringBuffer和StringBuleder是可變字元序列,在單線程時優先採用StirngBuleder

   *從線程角度考慮:StringBuleder線程不安全,不同步,執行效率高,比StringBuffer快

    *String和StringBuffer作為形式參數和基本類型是一樣的 


 *Integer類

     jdk5.0之後的新特性:自動拆裝箱,可變參數……

     每個基礎資料型別 (Elementary Data Type)都會被封裝成一個參考型別

     基本類型              參考型別

     int                 Integer

     char                Character

     byte                Byte

     long                Long

     double               Double

     short                Short

     float                Float

     boolean              Blooean


Integer類的構造方式:

  public Integer(int value):將一個int類型的資料封裝成一個參考型別

  public Integer(String s):將一個字元數類型封裝成一個Integer類型

     該字串必須是數字字串否則運行出錯:java.lang.NumberFormatException

   

package integer;public class IntegerDemo {public static void main(String[] args) {//public static String toBinaryString(int i)//以二進位無符號數形式返回一個整數參數的字串表示形式System.out.println(Integer.toBinaryString(100)) ;//public static String toOctalString(int i)//以八進位無符號數形式返回一個整數參數的字串表示形式System.out.println(Integer.toOctalString(100));//public static String toHexString(int i)//以十六進位無符號數形式返回一個整數參數的字串表示形式System.out.println(Integer.toHexString(100));//public static final int MAX_VALUE//int類型能夠表示的最大值//public static final int MIN_VALUE//int類型所能表示的最小值System.out.println(Integer.MAX_VALUE);//2147483647System.out.println(Integer.MIN_VALUE);//-2147483647}}


650) this.width=650;" src="https://s4.51cto.com/oss/201711/03/2e67c909b5b362e214f27c524e6b547f.png-wh_500x0-wm_3-wmp_4-s_3133664895.png" title="integer.png" alt="2e67c909b5b362e214f27c524e6b547f.png-wh_" />

  

   *int類型和String類型的轉換

     *public int intValue();//以int類型返回該Integer的值

     *public static int parseInt(String str);//將字串參數作為有符號數的十進位整數進行                                 解析


  *Character類

     *在對象中封裝一個基本類型char的值,Character類型的對象包含類型為char的欄位

     *構造方法:

           public Character(char value)://構造一個新分配的Character對象,來表示制定的                                char的值

     *Character的判斷功能和轉換功能

         

public static boolean isLowerCase(char ch)確定指定字元是否為小寫字母。

     *public static boolenn isUpperCase(char ch)確定指定字元是否為大寫字母

     *public static boolean isDigit(char ch)確定指定字元是否為數字。

     *public static char toUpperCase(char ch):將指定字元轉換成大寫

     *public static char toLowerCase(char ch):將指定字元轉換成小寫

 

 

package character;public class CharacterDemo {    public static void main(String[] args) {//public static boolean isLowerCase(char ch)確定指定字元是否為小寫字母System.out.println("isLowerCase:"+Character.isLowerCase(‘a‘));System.out.println("isLowerCase:"+Character.isLowerCase(‘A‘));System.out.println("isLowerCase:"+Character.isLowerCase(‘0‘));System.out.println("---------------------------------------");//public static boolenn isUpperCase(char ch)確定指定字元是否為大寫字母System.out.println("isUpperCase:"+Character.isUpperCase(‘a‘));System.out.println("isUpperCase:"+Character.isUpperCase(‘A‘));System.out.println("isUpperCase:"+Character.isUpperCase(‘0‘));System.out.println("---------------------------------------");//public static boolean isDigit(char ch)確定指定字元是否為數字。System.out.println("isDigit:"+Character.isDigit(‘a‘));System.out.println("isDigit:"+Character.isDigit(‘A‘));System.out.println("isDigit:"+Character.isDigit(‘0‘));}}

 


650) this.width=650;" src="https://s3.51cto.com/oss/201711/03/fbfacc6218a3c0f745455934b95007e7.png-wh_500x0-wm_3-wmp_4-s_1607661670.png" title="character.png" alt="fbfacc6218a3c0f745455934b95007e7.png-wh_" /> 

Java學習lesson 11

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.