字符
一般情況下,當我們處理字符時,我們用原始數據類型 char。
示例
1
2
3
4
5
6
7
|
char ch = 'a' ; // Unicode for uppercase Greek omega character char uniChar = '\u039A' ; // an array of chars char [] charArray ={ 'a' , 'b' , 'c' , 'd' , 'e' }; |
然而在開發中,我們會遇到需要使用對象而不是原始數據類型的情況。為了達到這個需求。Java 為原始數據類型 char 提供了包裝類 Character。
Character 類為操控字符提供了一系列有用處的類(例如:靜態類)。你可以借助 Character 構造函數創造一個 Character 對象。
1
|
Character ch = new Character( 'a' ); |
Java 編譯器也將能在某些情況下為你創造一個 Character 對象。例如:如果你將一個原始 char 傳輸到一個可預期對象的方法,編譯器就會為你自動將 char 轉化成 Character。 如果轉換從反方向進行,這個特點被稱之為自動裝箱或拆箱。
示例
1
2
3
4
5
6
7
8
|
// Here following primitive char 'a' // is boxed into the Character object ch Character 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 |
在文本中插入一個換頁。 |
\' |
在文本中插入一個單引號字符。 |
\\ |
在文本中插入一個反斜杠字符。 |
當一個轉義序列遇到一個打印語句,編譯器就會相應地解譯。
示例
如果你想把引號放入引號內,必須使用轉義序列, \” ,在內部引用:
1
2
3
4
5
6
|
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() |
2 |
isDigit() |
3 |
isWhitespace() |
4 |
isUpperCase() |
5 |
isLowerCase() |
6 |
toUpperCase() |
7 |
toLowerCase() |
8 |
toString() |
字符串
字符串,它被廣泛應用于 Java 編程,是一個字符序列。在 Java 編程語言中,字符串是對象。
Java 平臺提供了 String 類來創建和操作字符串。
創建字符串
最直接的方式來創建一個字符串是這樣寫的:
String greeting = "Hello world!";
當你創建一個字符串時,編譯器在這種情況下用它的值創建一個 String 對象,如:"Hello world!'。
任何其他對象可以通過使用 new 關鍵字,并通過構造函數創建 String 對象。 String 類有11種構造函數提供使用不同類型的字符串的初始值,如一個字符數組。
1
2
3
4
5
6
7
8
|
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 ); } } |
這將產生以下結果:
1
|
hello. |
注 String 類是不可變的,因此,一旦創建了 String 對象那么是不能改變的。如果需要大量修改字符的字符串,那么應該使用 StringBuffer & StringBuilder 類。
String 長度
用于獲取有關對象的信息的方法稱為存取方法。可以和字符串一起使用的一個存取方法是 length() ,它返回包含在字符串對象中的字符數。
下面的兩行代碼被執行之后,len 等于17:
1
2
3
4
5
6
7
8
|
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 ); } } |
這將產生以下結果:
1
|
String Length is : 17 |
連接字符串
String類包括用于連接兩個字符串的方法:
1
|
string1.concat(string2); |
這返回一個新的字符串,即在 string1 結尾處添加 string2。還可以使用 concat()方法連接字符串,如:
1
|
"My name is " .concat( "Zara" ); |
字符串更常使用 “ + ” 運算符連接在一起,如:
1
|
"Hello," + " world" + "!" |
這將產生:
1
|
"Hello, world!" |
看看下面的例子:
1
2
3
4
5
6
7
|
public class StringDemo { public static void main(String args[]) { String string1 = "saw I was " ; System.out.println( "Dot " + string1 + "Tod" ); } } |
這將產生以下結果:
1
|
Dot saw I was Tod |
創建格式化字符串
已經有 printf() 和 format() 方法來打印輸出格式的數字。 String 類有一個等價的方法 format(),它返回一個 String 對象,而不是一個 PrintStream 對象。
使用字符串的靜態 format() 方法允許創建可重復使用的格式化字符串,而不是一次性的 print 語句。例如,如果代替以下方法:
1
2
3
4
|
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); |
可以這樣寫:
1
2
3
4
5
6
|
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) |
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() |
13 |
byte[] getBytes(String charsetName |
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) |
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) |
42 |
String toString() |
43 |
String toUpperCase() |
44 |
String toUpperCase(Locale locale) |
45 |
String trim() |
46 |
static String valueOf(primitive data type x) |