字符
一般情況下,當我們處理字符時,我們用原始數據類型char。
示例
char ch = 'a';// Unicode for uppercase Greek omega characterchar uniChar = '/u039A'; // an array of charschar[] charArray ={ 'a', 'b', 'c', 'd', 'e' };然而在開發中,我們會遇到需要使用對象而不是原始數據類型的情況。為了達到這個需求。 Java 為原始數據類型char 提供了包裝類Character。
Character 類為操控字符提供了一系列有用處的類(例如:靜態類)。你可以藉助Character 構造函數創造一個Character 對象。
Character ch = new Character('a');Java 編譯器也將能在某些情況下為你創造一個Character 對象。例如:如果你將一個原始char 傳輸到一個可預期對象的方法,編譯器就會為你自動將char 轉化成Character。 如果轉換從反方向進行,這個特點被稱之為自動裝箱或拆箱。
示例
// Here following primitive char 'a'// is boxed into the Character object chCharacter ch = 'a';// Here primitive 'x' is boxed for method test,// return is unboxed to char 'c'char c = test('x');轉義序列
有反斜杠(/)在前的字符是一個轉義序列並且對於編譯器有特殊的意義。
換行符(/n)在System.out.println() 語句中經常使用,在字符串打印出來後換行。
以下的表格展示了Java 轉義序列:
| 轉義序列 | 描述 |
|---|---|
| /t | 在文本中插入一個標籤。 |
| /b | 在文本中插入一個退格。 |
| /n | 在文本中插入一個換行符。 |
| /r | 在文本中插入一個回車。 |
| /f | 在文本中插入一個換頁。 |
| /' | 在文本中插入一個單引號字符。 |
| // | 在文本中插入一個反斜杠字符。 |
示例
如果你想把引號放入引號內,必須使用轉義序列, /” ,在內部引用:
public class Test { public static void main(String args[]) { System.out.println("She said /"Hello!/" to me."); }}這將產生以下結果:
She said "Hello!" to me.
Character 方法以下列表是實現Character 類所有子類的重要的實例方法:
| SN | 方法描述 |
|---|---|
| 1 | isLetter() 確定具體的char值是一個字母 |
| 2 | isDigit() 確定具體的char值是一個數字 |
| 3 | isWhitespace() 確定具體的char值是一個空格 |
| 4 | isUpperCase() 確定具體的char值是一個大寫字母 |
| 5 | isLowerCase() 確定具體的char值是一個小寫字母 |
| 6 | toUpperCase() 返回指定字符值的大寫形式 |
| 7 | toLowerCase() 返回指定字符值的小寫寫形式 |
| 8 | toString() 返回代表指定的字符值的一個String對象,即一個字符的字符串 |
字符串
字符串,它被廣泛應用於Java 編程,是一個字符序列。在Java 編程語言中,字符串是對象。
Java 平台提供了String 類來創建和操作字符串。
創建字符串
最直接的方式來創建一個字符串是這樣寫的:
String greeting = "Hello world!";
當你創建一個字符串時,編譯器在這種情況下用它的值創建一個String 對象,如:"Hello world!'。
任何其他對象可以通過使用new 關鍵字,並通過構造函數創建String 對象。 String 類有11種構造函數提供使用不同類型的字符串的初始值,如一個字符數組。
public class StringDemo{ public static void main(String args[]){ char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.'}; String helloString = new String(helloArray); System.out.println( helloString ); }}這將產生以下結果:
hello.
注String 類是不可變的,因此,一旦創建了String 對像那麼是不能改變的。如果需要大量修改字符的字符串,那麼應該使用StringBuffer & StringBuilder 類。
String 長度
用於獲取有關對象的信息的方法稱為存取方法。可以和字符串一起使用的一個存取方法是length() ,它返回包含在字符串對像中的字符數。
下面的兩行代碼被執行之後,len 等於17:
public class StringDemo { public static void main(String args[]) { String palindrome = "Dot saw I was Tod"; int len = palindrome.length(); System.out.println( "String Length is : " + len ); }}這將產生以下結果:
String Length is : 17
連接字符串
String類包括用於連接兩個字符串的方法:
string1.concat(string2);
這返回一個新的字符串,即在string1 結尾處添加string2。還可以使用concat()方法連接字符串,如:
"My name is ".concat("Zara");字符串更常使用“ + ” 運算符連接在一起,如:
"Hello," + " world" + "!"
這將產生:
"Hello, world!"
看看下面的例子:
public class StringDemo { public static void main(String args[]) { String string1 = "saw I was "; System.out.println("Dot " + string1 + "Tod"); }}這將產生以下結果:
Dot saw I was Tod
創建格式化字符串
已經有printf() 和format() 方法來打印輸出格式的數字。 String 類有一個等價的方法format(),它返回一個String 對象,而不是一個PrintStream 對象。
使用字符串的靜態format() 方法允許創建可重複使用的格式化字符串,而不是一次性的print 語句。例如,如果代替以下方法:
System.out.printf("The value of the float variable is " + "%f, while the value of the integer " + "variable is %d, and the string " + "is %s", floatVar, intVar, stringVar);可以這樣寫:
String fs;fs = String.format("The value of the float variable is " + "%f, while the value of the integer " + "variable is %d, and the string " + "is %s", floatVar, intVar, stringVar);System.out.println(fs); String 方法
這裡是由String 類支持的方法列表:
| SN | 方法及描述 |
|---|---|
| 1 | char charAt(int index) 返回指定索引處的字符。 |
| 2 | int compareTo(Object o) 將這個字符串與另一個對像比較。 |
| 3 | int compareTo(String anotherString) 比較兩個字符串的字典順序。 |
| 4 | int compareToIgnoreCase(String str) 比較兩個字符串按字典順序,不區分大小寫的差異。 |
| 5 | String concat(String str) 將指定的字符串串連到這個字符串的結尾。 |
| 6 | boolean contentEquals(StringBuffer sb) 返回true當且僅當該字符串代表相同的字符序列作為指定的StringBuffer。 |
| 7 | static String copyValueOf(char[] data) 返回表示所指定的數組中的字符序列的字符串。 |
| 8 | static String copyValueOf(char[] data, int offset, int count) 返回表示所指定的數組中的字符序列的字符串。 |
| 9 | boolean endsWith(String suffix) 測試此字符串是否以指定的後綴結束。 |
| 10 | boolean equals(Object anObject) 比較此字符串與指定的對象。 |
| 11 | boolean equalsIgnoreCase(String anotherString) 比較這個字符串到另一個字符串,忽略大小寫的考慮。 |
| 12 | byte getBytes() 將此String解碼使用平台的默認字符集,並將結果存儲到一個新的字節數組中的字節序列。 |
| 13 | byte[] getBytes(String charsetName 將此String解碼使用指定的字符集的字節序列,並將結果存儲到一個新的字節數組。 |
| 14 | void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 從這個字符串複製字符到目標字符數組。 |
| 15 | int hashCode() 為這個字符串返回一個哈希代碼。 |
| 16 | int indexOf(int ch) 返回此字符串指定字符第一次出現處的索引。 |
| 17 | int indexOf(int ch, int fromIndex) 返回索引這個字符串中指定字符第一次出現處,指定索引處開始搜索。 |
| 18 | int indexOf(String str) 返回此字符串指定子字符串的第一次出現處的索引。 |
| 19 | int indexOf(String str,int fromIndex) 返回這個字符串中指定子字符串的第一次出現處的索引,從指定的索引處開始。 |
| 20 | String intern() 返回字符串對象的規範化表示。 |
| 21 | int lastIndexOf(int ch) 返回此字符串指定字符最後一次出現處的索引。 |
| 22 | int lastIndexOf(int ch, int fromIndex) 返回此字符串指定字符最後一次出現處的索引,從指定索引開始向後搜索。 |
| 23 | int lastIndexOf(String str) 返回此字符串指定子字符串的最右邊出現處的索引。 |
| 24 | int lastIndexOf(String str, int fromIndex) 返回索引這個字符串中指定子字符串的最後出現處,從指定的索引開始處向後搜索。 |
| 25 | int length() 返回此字符串的長度。 |
| 26 | boolean matches(String regex) 判斷此字符串是否與給定的正則表達式匹配。 |
| 27 | boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) 測試兩個字符串的區域等於。 |
| 28 | boolean regionMatches(int toffset, String other, int ooffset, int len) 測試兩個字符串的區域都是相等的。 |
| 29 | String replace(char oldChar, char newChar) 返回從newChar更換oldChar所有出現在此字符串中產生一個新的字符串。 |
| 30 | String replaceAll(String regex, String replacement 替換此字符串中給定的正則表達式與給定替換相匹配的每個子字符串。 |
| 31 | String replaceFirst(String regex, String replacement) 替換此字符串匹配給定的正則表達式給定替換第一個子字符串。 |
| 32 | String[] split(String regex) 分割圍繞給定的正則表達式匹配的這個字符串。 |
| 33 | String[] split(String regex, int limit) 分割圍繞給定的正則表達式匹配的這個字符串。 |
| 34 | boolean startsWith(String prefix) 測試此字符串是否以指定的前綴開頭。 |
| 35 | boolean startsWith(String prefix, int toffset) 測試此字符串是否以指定索引開始的指定前綴開始。 |
| 36 | CharSequence subSequence(int beginIndex, int endIndex) 返回一個新的字符序列,這個序列的子序列。 |
| 37 | String substring(int beginIndex) 返回一個新的字符串,它是此字符串的一個子字符串。 |
| 38 | String substring(int beginIndex, int endIndex) 返回一個新的字符串,它是此字符串的一個子字符串。 |
| 39 | char[] toCharArray() 這個字符串轉換為一個新的字符數組。 |
| 40 | String toLowerCase() 將所有在此字符串中的字符使用默認語言環境的規則小寫。 |
| 41 | String toLowerCase(Locale locale) 將所有在此字符串中的字符使用給定Locale的規則小寫。 |
| 42 | String toString() 這個對象(它已經是一個字符串!)返回字符串形式(這裡是自己本身)。 |
| 43 | String toUpperCase() 使用默認語言環境的規則將此String中所有的字符轉換為大寫。 |
| 44 | String toUpperCase(Locale locale) 使用給定Locale的規則將此String中所有的字符轉換為大寫。 |
| 45 | String trim() 返回字符串的一個副本,開頭和結尾的空格去除。 |
| 46 | static String valueOf(primitive data type x) 返回傳遞的數據類型參數的字符串表示形式。 |