在項目實際開發過程中,只要是項目就一定會存在String類的定義。
1JavaDoc文檔簡介
在進行開發的過程之中肯定要大量的去使用Java的API文檔(JavaDoc),這個文檔可以直接通過oracle的在線訪問進行查看。
在JDK 1.9之前,所有的Java中的常用類庫都會在JVM啓動的時候進行全部的加載,這樣的話性能會有所下降,在JDK 1.9之後提供有模塊化的設計,將一些程序類放在了不同的模塊裏面。在模塊中會包含有大量的程序開發包。
一般文檔裏的組成:
- 類的完整定義;
- 類相關說明信息;
- 成員屬性摘要;
- 構造方法摘要,如果看見有deprecated描述的方法表示不建議使用。
- 方法摘要:左邊爲返回值,右邊爲方法名稱和相應的參數。
- 方法和成員的詳細解釋了。
2 字符串與字符
在JDK 1.9一前,所有的String都利用了字符數組實現了包裝的處理,所以在String類裏面是提供有相應的轉換處理方法的。這些方法包含構造方法和普通方法兩類。
No. | 名稱 | 類型 | 描述 |
---|---|---|---|
1 | public String(char[] value) | 構造 | 將傳入的字符數組變爲字符串 |
2 | public String(int[] codePoints, int offset, int count) | 構造 | 將部分字符數組變爲字符串 |
3 | public char charAt(int index) | 普通 | 獲取指定索引位置的字符 |
4 | public char[] toCharArray() | 普通 | 將字符串中的數據以字符數組的形式返回 |
在實際開發之中處理中文的時候往往使用char類型,因爲其包含中文數據。
public class StringDemo{
public static void main(String[] args){
char[] ch = new char[]{'1','2','3','4','5'};
String strA = new String(ch);
String strB = new String(ch, 1, 2);
System.out.println(strA);
System.out.println(strB);
}
}
3 字符串與字節
當進行了字符串與字節轉換時,其主要目的是爲了進行二進制的數據傳輸或者是進行編碼轉換。
No. | 名稱 | 類型 | 描述 |
---|---|---|---|
1 | public String(byte[] bytes) | 構造 | 將全部字節數組變爲字符串 |
2 | public String(byte[] bytes, int offset, int length) | 構造 | 將部分字節數組變爲字符串 |
3 | public byte[] getBytes() | 普通 | 將字符串轉爲字節數組 |
4 | public byte[] getBytes(String charsetName) throws UnsupportedEncodingException | 普通 | 編碼轉換 |
public class StringDemo{
public static void main(String[] args){
byte[] bytes = new byte[]{'1','2','3','4','5'};
String strA = new String(bytes);
String strB = new String(bytes, 1, 2);
System.out.println(strA);
System.out.println(strB);
}
}
4 字符串比較
字符串比較裏面最爲常用的方法就是equals方法,但是這個方法需要注意的是其進行大小寫區分的。
No. | 名稱 | 類型 | 描述 |
---|---|---|---|
1 | public boolean equals(String anObject) | 普通 | 區分大小寫的相等判斷 |
2 | public boolean equalsIgnoreCase(String anotherString) | 普通 | 不區分大小寫的相等判斷 |
3 | public int compareTo(String anotherString) | 普通 | 進行字符串大小比較,該方法返回一個int數據,該數據有三種取值:大於0、小於0、等於0 |
4 | public int compareToIgnoreCase(String str) | 普通 | 不區分大小寫進行字符串大小比較 |
5 字符串查找
No. | 名稱 | 類型 | 描述 |
---|---|---|---|
1 | public boolean contains(CharSequence s) | 普通 | 判斷字符串是否存在 |
2 | public int indexOf(String str) | 普通 | 從頭查找指定字符串的位置,找不到返回-1,在開發之中利用此形式來進行索引的位置的確定 |
3 | public int indexOf(String str, int fromIndex) | 普通 | 從指定位查找,由前向後 |
4 | public int lastIndexOf(String str) | 普通 | 從末尾查找指定字符串的位置,找不到返回-1 |
5 | public int lastIndexOf(String str, int fromIndex) | 普通 | 從指定位查找,由後向前 |
6 | public boolean startsWith(String prefix) | 普通 | 判斷是否以指定字符開頭 |
7 | public boolean endsWith(String suffix) | 普通 | 判斷是否以指定字符結尾 |
6 字符串替換
No. | 名稱 | 類型 | 描述 |
---|---|---|---|
1 | public String replaceAll(String regex, String replacement) | 普通 | 用指定字符替換字符串中的所有指定字符 |
2 | public String replaceFirst(String regex, String replacement) | 普通 | 用指定字符替換字符串中的第一個指定字符 |
7 字符串拆分
No. | 名稱 | 類型 | 描述 |
---|---|---|---|
1 | public String[] split(String regex) | 普通 | 根據指定字符拆分字符串 |
2 | public String[] split(String regex, int limit) | 普通 | 根據指定字符拆分字符串,不夠拆就不拆了 |
遇見拆不了的情況,這個時候最簡單的理解就是使用\進行轉義。
8 字符串截取
No. | 名稱 | 類型 | 描述 |
---|---|---|---|
1 | public String substring(int beginIndex) | 普通 | 從字符串的指定位置截取子串 |
9 字符串格式化
No. | 名稱 | 類型 | 描述 |
---|---|---|---|
1 | public static String format(String format, Object… args) | 普通 | 格式化輸出 |
10 其他操作方法
concat() 連接字符串,注意區別+號。
isEmpty() 判斷字符串是否爲空串,不是null。
length() 返回字符串長度。
trim() 去除字符串首尾空格。
toUpperCase() 字母變爲大寫。
toLowerCase() 字母變爲小寫。
首字母變爲大寫代碼:
public class StringDemo{
public static String capitalize(String str){
if( str.isEmpty() || str == null ){
return str;
}
if( str.length() == 1 ){
return str.toUpperCase();
}
return str.substring(0,1).toUpperCase() + str.substring(1);
}
public static void main(String[] args){
System.out.println(StringDemo.capitalize("Hello"));
System.out.println(StringDemo.capitalize("m"));
}
}