String
Java程式中所有的字元串文字(例如
"abc"
)都可以被看作是實作此類的執行個體。
特點:
1、字元串String類型本身是final聲明的,意味着我們不能繼承String。
2、字元串的對象也是不可變對象,意味着一旦進行修改,就會産生新對象
我們修改了字元串後,如果想要獲得新的内容,必須重新接收。
3、String對象内部是用字元數組進行儲存的
JDK1.9之前有一個char[] value數組,JDK1.9之後byte[]數組
4、String類中這個char[] values數組也是final修飾的,意味着這個數組不可變,然後它是private修飾,外部不能直接操作它,String類型提供的所有的方法都是用新對象來表示修改後内容的,是以保證了String對象的不可變。
5、就因為字元串對象設計為不可變,那麼是以字元串有常量池來儲存很多常量對象,可以共享。
如果涉及到大量的字元串修改操作,建議使用StringBuffer或StringBuilder
String對象的建立
1、字面常量值
String str = "hello";
2、使用構造器
-
:初始化新建立的 String對象,以使其表示空字元序列。public String()
-
: 初始化一個新建立的String(String original)
對象,使其表示一個與參數相同的字元序列;換句話說,新建立的字元串是該參數字元串的副本。String
-
:通過目前參數中的字元數組來構造新的String。public String(char[] value)
-
:通過字元數組的一部分來構造新的String。public String(char[] value,int offset, int count)
-
:通過使用平台的預設字元集解碼目前參數中的位元組數組來構造新的String。public String(byte[] bytes)
-
:通過使用指定的字元集解碼目前參數中的位元組數組來構造新的String。public String(byte[] bytes,String charsetName)
3、靜态方法
- static String copyValueOf(char[] data): 傳回指定數組中表示該字元序列的 String
- static String copyValueOf(char[] data, int offset, int count):傳回指定數組中表示該字元序列的 String
- static String valueOf(char[] data) : 傳回指定數組中表示該字元序列的 String
- static String valueOf(char[] data, int offset, int count) : 傳回指定數組中表示該字元序列的 String
- static String valueOf(xx value):xx支援各種資料類型,傳回各種資料類型的value參數的字元串表示形式。
4、xx.toString()
StringBuffer s = new StringBuffer(xx);
String str = s.toString(); :傳回字元串對象
5、和字元串的拼接+
任意資料類型與"字元串"進行拼接,結果都是字元串
String對象的個數
String str = "hello"; //1個
String str = new String("atguigu"); //2個
String s1 = "hello";//1個
String s2 = "world";//1個
String s3 = s1 + s2 + "java"; //"java"1個,s1 + s2拼接結果1個,最後結果1個
字元串對象的記憶體分析

String對象的拼接
1、+
(1)常量+常量:結果是常量池
(2)常量與變量 或 變量與變量:結果是堆
指向堆中的字元串常量 + 常量:結果都在堆
(3)拼接後調用intern方法:結果在常量池
2、concat:拼接的結果都是新的字元串,哪怕是兩個常量對象拼接,都在堆中
String s1 = "hello";
String s2 = "world";
String s3 = s1 + "world"; //s3字元串内容也是helloworld,s1是變量,"world"常量,變量 + 常量的結果在堆中
String s4 = s1 + s2; //s4字元串内容也是helloworld,s1和s2都是變量,變量 + 變量的結果在堆中
String對象的比較
1、==:比較位址
2、equals(xx):比較字元串内容,嚴格區分大小寫。因為String類型重寫equals
3、equalsIgnoreCase(xx):比較字元串内容,不區分大小寫
4、compareTo(xx):比較字元串的大小,按照字元編碼值比較,嚴格區分大小寫。String類型重寫了Comparable接口的抽象方法,自然排序
5、compareToIgnoreCase(xx):比較字元串的大小,按照字元編碼值比較,不區分大小寫
6、java.text.Collator:文本校對器比較大小,按照指定語言環境的自然語言順序比較大小(字典排序)
空字元的比較
空字元串
String str1 = "";
String str2 = new String();
String str3 = new String("");
判斷某個字元串是否是空字元串
if("".equals(str)) //推薦
if(str!=null && str.isEmpty())
if(str!=null && str.equals(""))
if(str!=null && str.length()==0)
String類的常用API
序号 | 方法簽名 | 方法功能簡介 |
---|---|---|
1 | String() | 建立空字元串 |
2 | String(String original) | 根據original建立一個新字元串 |
3 | static String valueOf(xx value) | 根據value内容建立一個字元串 |
4 | String intern() | 将字元串的内容存入常量池 |
5 | String concat(String s) | 字元串拼接 |
6 | boolean equals(Object obj) | 判斷目前字元串與指定字元串内容是否相同,嚴格區分大小寫 |
7 | boolean equalsIgnoreCase(String obj) | 判斷目前字元串與指定字元串内容是否已在,不區分大小寫 |
8 | int compareTo(String str) | 比較目前字元串與指定字元串的大小,嚴格區分大小寫 |
9 | int compareToIgnoreCase(String str) | 比較目前字元串與指定字元串的大小,不區分大小寫 |
10 | boolean isEmpty() | 判斷目前字元串是否為空 |
11 | int length() | 傳回目前字元串的長度 |
12 | String toLowerCase() | 将目前字元串轉為小寫 |
13 | String toUpperCase() | 将目前字元串轉為大寫 |
14 | String trim() | 去掉目前字元串前後空白符 |
15 | boolean contains(xx) | 判斷目前字元串中是否包含xx |
16 | int indexOf(xx) | 在目前字元串中查找xx第一次出現的下标 |
17 | int lastIndexOf(xx) | 在目前字元串中查找xx最後一次出現的下标 |
18 | String substring(int beginIndex) | 從目前字元串的[beginIndex, 最後]截取一個子串 |
19 | String substring(int beginIndex, int endIndex) | 從目前字元串的[beginIndex, endIndex)截取一個子串 |
20 | char charAt(index) | 傳回目前字元串[index]位置字元 |
21 | char[] toCharArray() | 将目前字元串的内容用一個字元數組傳回 |
22 | String(char[] value) | 用value字元數組的元素建構一個新字元串 |
23 | String(char[] value,int offset, int count) | 用value字元數組的[offset]開始的count個字元建構一個新字元串 |
24 | static String copyValueOf(char[] data) | 用data字元數組的元素建構一個新字元串 |
25 | static String copyValueOf(char[] data, int offset, int count) | 用data字元數組的[offset]開始的count個字元建構一個新字元串 |
26 | static String valueOf(char[] data) | |
27 | static String valueOf(char[] data, int offset, int count) | |
28 | byte[] getBytes() | 将目前字元串按照平台預設字元編碼方式編碼為位元組序列 |
29 | byte[] getBytes(字元編碼方式) | 将目前字元串按照指定字元編碼方式編碼為位元組序列 |
30 | String(byte[] bytes) | 将bytes位元組序列按照平台預設字元編碼方式解碼為字元串 |
31 | String(byte[] bytes,String charsetName) | 将bytes位元組序列按照指定字元編碼方式解碼為字元串 |
32 | boolean startsWith(xx) | 判斷目前字元串是否以xx開頭 |
33 | boolean endsWith(xx) | 判斷目前字元串是否以xx結尾 |
34 | boolean matchs(xx) | 判斷目前字元串是否滿足xx正則 |
35 | String replace(xx,yy) | 将目前字元串中所有xx替換為yy |
36 | String replaceFirst(xx,value) | 将目前字元串中第一個滿足xx正則的字元替換為value |
37 | String repalceAll(xx, value) | 将目前字元串中所有滿足xx正則的字元替換為value |
38 | String[] split(xx) | 将目前字元串按照xx正則拆分為多個字元串 |
39 | void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) | 将目前字元串的[srtBegin,srcEnd)部分字元複制到dst字元數組中,dst數組從[dstBegin]開始存儲 |
StringBuffer和StringBuilder
String類的對象是不可變字元序列,StringBuffer和StringBuilder的對象是可變字元序列。
StringBuffer:老點,線程安全的,因為它的方法有synchronized修飾
StringBuilder:JDK1.5之後引入的,線程不安全,單線程情況下推薦使用。
常用的API,StringBuilder、StringBuffer的API是完全一緻的
StringBuilder、StringBuffer的API
方法區功能簡介 | ||
---|---|---|
StringBuffer() | 建立一個空的可變字元序列,預設長度16 | |
StringBuffer(String str) | 用字元串str内容建立一個可變字元序列 | |
StringBuffer append(資料類型 b) | 在目前字元序列後面追加b | |
StringBuffer insert(int index, 資料類型 s) | 在目前字元序列[index]插入s | |
StringBuffer delete(int start, int end) | 删除目前字元序列[start,end)部分字元 | |
StringBuffer deleteCharAt(int index) | 删除目前字元序列[index]位置字元 | |
void setLength(int newLength) | 修改目前字元序列的長度為newLength | |
void setCharAt(int index, char ch) | 替換目前字元序列[index]位置字元為ch | |
StringBuffer reverse() | 将目前字元序列内容反轉 | |
StringBuffer replace(int start, int end, String str) | 替換目前字元序列[start,end)部分字元為str | |
int indexOf(String str) | 在目前字元序列中開始查找str第一次出現的下标 | |
int indexOf(String str, int fromIndex) | 在目前字元序列[fromIndex]開始查找str第一次出現的下标 | |
int lastIndexOf(String str) | 在目前字元序列中開始查找str最後一次出現的下标 | |
int lastIndexOf(String str, int fromIndex) | 在目前字元序列[fromIndex]開始查找str最後一次出現的下标 | |
String substring(int start) | 截取目前字元序列[start,最後]部分構成一個字元串 | |
String substring(int start, int end) | 截取目前字元序列[start,end)部分構成一個字元串 | |
String toString() | 将目前可變字元序列的内容用String字元串形式表示 | |
void trimToSize() | 如果緩沖區大于儲存目前字元序列所需的存儲空間,則将重新調整其大小,以便更好地利用存儲空間。 | |
傳回目前字元序列的長度 | ||
char charAt(int index) | 傳回目前字元序列[index]位置字元 | |
正規表達式
字元編碼
ASCII碼
- 0~31及127(共33個)是控制字元或通信專用字元(其餘為可顯示字元),如控制符:LF(換行)、CR(回車)、FF(換頁)、DEL(删除)、BS(倒退)
- 32~126(共95個)是字元(32是空格),其中48~57為0到9十個阿拉伯數字。
- 65~90為26個大寫英文字母,97~122号為26個小寫英文字母,其餘為一些标點符号、運算符号等。
Unicode
為解決一個問題:如果一份文檔中含有不同國家的不同語言的字元,那麼無法在一份文檔中顯示所有字元。Unicode字元集涵蓋了目前人類使用的所有字元,并為每個字元進行統一編号,配置設定唯一的字元碼(Code Point)
UTF-8
它是一種變長的編碼方式。它可以使用1~4個位元組表示一個符号。從unicode到uft-8并不是直接的對應,而是要過一些算法和規則來轉換(即Uncidoe字元集≠UTF-8編碼方式)
Unicode隻是定義了一個龐大的、全球通用的字元集,并為每個字元規定了唯一确定的編号,具體存儲成什麼樣的位元組流,取決于字元編碼方案。推薦的Unicode編碼是UTF-16和UTF-8。