1. 程式人生 > 其它 >Java常用類學習:String類

Java常用類學習:String類

Java常用類學習:String類

  • Java String類:

    • 在Java中:字串屬於物件,Java提供了String類來建立和操作字串;

       

  • 建立字串:

    String name="haha";

    //1,在程式碼中遇到字串常量時(這裡指的是haha),編譯器會使用該值建立一個String物件;
    //2,和其他物件一樣,可以使用關鍵字和構造方法來建立String物件;

    //3,用構造方法建立字串
    String name1=new String("haha");

    //4,String建立的字串,儲存在公共池中,而new建立的字串物件在堆中;

    //例子:
    String s1="hello";
    String s2="hello";
    String s3=s2;
    String s4=new String("hello");
    String s5=new String("hello");

     

  • String類有11種構造方法,這些方法提供不同的引數來初始化字串;

  • String類是不可改變的,所以一旦建立了String物件,那它的值就無法改變了;

  • 如果要對字串頻繁修改,需要使用StringBuffer和StringBuilde類;

    String s="Google";
    System.out.println("s:"+s);

    s="Runoob";
    System.out.println("s:"+s);

    /**
    筆記:
    1,從結果上看,是改變了;但為什麼說String物件是不可變的呢?
    2,原因在於例項中的s只是一個String物件的引用,並不是物件本身;
    3,當執行s="baidu";建立了一個新的物件"baidu",而原來的"Google"還存在記憶體中;

    */

     

  • 字串長度:

    • 用於獲取有關物件的資訊的方法稱為訪問器方法

    • String類的一個訪問器方法是length()方法,它返回字串物件包含的字元數;

      public class StringDemo01 {
         public static void main(String[] args) {

             String s="Google";
             int i=s.length();
             System.out.println(i);//6

        }
      }

       

  • 字串連線:

    • String類提供了連線2個字串的方法;

    • 切記:用新的字串去接收;

      public class StringDemo01 {
         public static void main(String[] args) {

             String str="Google";
             String str1=".com";
             String str3=str.concat(str1);//切記,用新的字串去接收
             System.out.println(str3);//Google.com

        }
      }

       

  • 程式碼案例:字串常用方法

    • 注意1:isEmpty() :判斷的是:value.length == 0;

    • 注意2:String[] strArr=s12.split("\.");//切記:特殊符號需要轉義


    /**
    * 字串常用方法
    */
    public class StringDemo01 {
       public static void main(String[] args) {
           String str="Google";

           //int codePointAt(int index):返回指定索引處的字元(Unicode值)
           int i1=str.codePointAt(0);
           System.out.println(i1);//71

           //int compareTo(String str):按字典順序比較2個字串
           String str1="Google";
           int i2=str.compareTo(str1);
           System.out.println(i2);//全匹配:0;否則不為0

           //boolean endWith(String str):判斷此字串是否以指定的字尾結尾
           Boolean b=str.endsWith("e");
           System.out.println(b);//true

           //boolean startWith(String str):判斷此字串是否以指定的字首開始
           Boolean b1=str.startsWith("G");
           System.out.println(b1);

           //boolean equals(String str):將此字串與指定物件進行比較
           boolean b2=str.equals(str1);
           System.out.println(b2);//true

           //boolean equalsIgnorecase(String s):忽略大小寫比較字串
           boolean b3=str.equalsIgnoreCase("GoogLE");
           System.out.println(b3);//true

           //static String format(String format,Object obj):格式化字串
           //String str3= String.format(str, )


           //int hashCode(String str):返回該字串的hashCode值
           int i3=str.hashCode();
           System.out.println(i3);//2138589785

           //char charAt(int index):返回char指定索引處的值

           char c=str.charAt(0);
           System.out.println(c);//G

           //int indexOf(char c):
           //System.out.println(str.toString());
           int i4=str.indexOf('G');
           System.out.println(i4);//0

           //int length(String str):
           int i5=str.length();
           System.out.println(i5);//6

           //String replace(char c1,char c2):替換:用新的字元替換舊的字元
           String s11="Google";
           s11=s11.replace('G','g');
           System.out.println(s11);//google

           //String[] split(String regex):
           String s12="www.baidu.com";
           String[] strArr=