1. 程式人生 > >java學習筆記——String類

java學習筆記——String類

通過 ray [] 原理 log spl 2.3 -s 長度

一、概述

·字符串是一個特殊的對象

·字符串一旦初始化就不可以被改變

·String str = "abc";

·String str1 = new String("abc");

有什麽區別?

package com.java.study.StringDemo;

public class StringDemo {
    
    public static void main(String[] args) {
        
        String s1 = "abc"; //s1是一個類類型變量,"abc"是一個對象
                           //字符串最大特點:一旦被初始化就不可以被改變
String s2 = new String("abc"); //s1和s2有什麽區別? //s1在內存中有一個對象 //s2在內存中有兩個對象 System.out.println(s1==s2); System.out.println(s1.equals(s2));//String類復寫了Object類中的equals方法 //該方法用於判斷字符串是否相同 } }

二、常見功能獲取和判斷

String類適用於描述字符串事物,那麽它就提供了多個方法對字符串進行操作

常見的操作有哪些?

"abcd"

1.獲取

  1.1 字符串中包含的字符數,也就是字符串的長度

    int length():獲取長度

  1.2 根據位置獲取位置上的某個字符

    char chartAt(int index)

  1.3 根據字符獲取該字符在字符串中的位置

    int indexOf(int ch):返回的是ch在字符串中第一次出現的位置

    int indexOf(int ch,int fromIndex):從fromIndex指定位置開始,獲取ch在字符串中出現的位置

    int indexOf(String str):返回的是str在字符串中第一次出現的位置

    int indexOf(String str,int fromIndex):從fromIndex指定位置開始,獲取str在字符串中出現的位置

2.判斷

  2.1 字符串中是否包含某一個子串

    boolean contains(str)

    特殊之處:indexOf(str):可以索引str第一次出現的位置,如果返回-1,表示str不在字符串中存在

         所以,也可以用於對指定判斷是否包含

         if(str.indexOf("aa")!=-1) 而且該方法既可以用於判斷,又可以獲取出現的位置

  2.2 字符串中是否有內容

    boolean isEmpty();原理就是判斷長度是否為0

  2.3 字符串是否以指定內容開頭

    boolean startsWith(str);

  2.4 字符串是否以指定內容結尾

    boolean endsWith(str);

  2.5 判斷字符串的內容是否相同,復寫了Object類中的equals方法

    boolean equals(str)

  2.6 判斷內容是否相同,並忽略大小寫

    boolean equalsIgnoreCase();

3.轉換

  3.1 將字符數組轉成字符串

    構造函數:String(char[]);

         String(char[],offset,count):將字符數組中的一部分轉成字符串 

    靜態方法:

        static String copyValueOf(char[]);

        copyValueOf(char[],int offset,int count);

  3.2 將字符串轉成字符數組

        char[] toCharArray()

  3.3 將字節數組轉成字符串     

         String(byte[]);

         String(byte[],offset,count):將字節數組中的一部分轉成字符串 

  3.4 將字符串轉成字節數組

        byte[] getBytes()

  3.5 將基本數據類型轉成字符串

    static String valueOf(int)

    static String valueOf(double)

4. 替換

  String replace(oldchar,newchar);  

5.切割

  String[] split(regex);

6.子串 獲取字符串中的一部分

  String substring(begin);//從指定位置開始到結尾

  String substring(begin,end);//包含頭 不包含尾

7.轉換,去除空格,比較

  7.1 將字符串轉成大寫或者小寫

    String toUpperCase();

    String toLowerCase();

  7.2 將字符串兩端的多個空格去除

    String trim();

  7.3 對兩個字符串進行自然順序的比較

    String compareTo(string);

 1 package com.java.study.StringDemo.StringMethodDemo;
 2 
 3 public class StringMethodDemo {
 4     
 5     public static void method_7() {
 6         String s = "     hello  JAVA   ";
 7         sop(s.toUpperCase());
 8         sop(s.toLowerCase());
 9         sop(s.trim());
10         
11         String s1 = "abc";
12         String s2 = "aaa";
13         sop(s1.compareTo(s2));
14         
15     }
16     
17     public static void method_sub() {
18         String s = "abcdef";
19         
20         sop(s.substring(2));
21         sop(s.subSequence(2, 4));
22     }
23     
24     public static void method_split() {
25         String s = "zhangsan,lisi,wangwu";
26         String[] arr = s.split(",");
27         for(int i=0;i<arr.length;i++) {
28             sop(arr[i]);
29         }
30     }
31     
32     public static void method_replace() {
33         String s = "hello java";
34         //String s1 = s.replace(‘a‘,‘n‘);//如果要替換的字符不存在 返回的還是原串
35         String s1 = s.replace("java", "world");
36         sop("s="+s);
37         sop("s1="+s1);
38     }
39     
40     public static void method_trans() {
41         char[] arr = {‘a‘,‘b‘,‘c‘,‘d‘,‘e‘,‘f‘};
42         String s = new String(arr);
43         
44         sop("s="+s);
45     }
46     
47     public static void method_is() {
48         String str = "ArrayDemo.java";
49         
50         //判斷文件名稱是否是Array單詞開頭
51         sop(str.startsWith("Array"));
52         //判斷文件名稱是否是.java單詞結尾
53         sop(str.endsWith(".java"));
54         //判斷文件名稱是否包含Demo單詞        
55         sop(str.contains("Demo"));
56         
57     }
58     
59     public static void method_get() {
60         String str = "abcdeakpf";
61         
62         //長度
63         sop(str.length());
64         
65         //根據索引獲取字符
66         sop(str.charAt(4));//當訪問到字符串中不存在的角標時,會發生StringIndexOutBoundsException.
67         
68         //根據字符獲取索引
69         sop(str.indexOf(‘a‘,3));//如果沒有找到,返回-1
70         
71         //反向索引一個字符出現的位置
72         sop(str.lastIndexOf(‘a‘));
73         
74     }
75     
76     
77     public static void main(String[] args) {
78         
79         method_7();
80         //method_sub();
81         //method_split();
82         //method_replace();
83         //method_trans();
84         //method_get();
85         //method_is();
86     
87     }
88     
89     public static void sop(Object obj) {
90         System.out.println(obj);
91     }
92     
93 }

三、StringBuffer

·字符串的組成原理就是通過該類實現的

·StringBuffer可以對字符串內容進行增刪

·StringBuffer是一個容器

·很多方法與String相同

·StringBuffer是可變長度的

StringBuffer是字符串緩沖區 是一個容器

特點:

·長度是可變的

·可以直接操作多個數據類型

·最終會通過toString方法變成字符串

1.存儲

  StringBuffer append();將指定數據作為參數添加到已有數據結尾處

  StringBuffer insert(index,數據);可以將數據插入到指定index位置

2.刪除

3.獲取

4.修改

java學習筆記——String類