字符
一般情況下,當(dāng)我們處理字符時(shí),我們用原始數(shù)據(jù)類型 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' }; |
然而在開發(fā)中,我們會(huì)遇到需要使用對象而不是原始數(shù)據(jù)類型的情況。為了達(dá)到這個(gè)需求。Java 為原始數(shù)據(jù)類型 char 提供了包裝類 Character。
Character 類為操控字符提供了一系列有用處的類(例如:靜態(tài)類)。你可以借助 Character 構(gòu)造函數(shù)創(chuàng)造一個(gè) Character 對象。
1
|
Character ch = new Character( 'a' ); |
Java 編譯器也將能在某些情況下為你創(chuàng)造一個(gè) Character 對象。例如:如果你將一個(gè)原始 char 傳輸?shù)揭粋€(gè)可預(yù)期對象的方法,編譯器就會(huì)為你自動(dòng)將 char 轉(zhuǎn)化成 Character。 如果轉(zhuǎn)換從反方向進(jìn)行,這個(gè)特點(diǎn)被稱之為自動(dòng)裝箱或拆箱。
示例
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' ); |
轉(zhuǎn)義序列
有反斜杠(\)在前的字符是一個(gè)轉(zhuǎn)義序列并且對于編譯器有特殊的意義。
換行符(\n)在 System.out.println() 語句中經(jīng)常使用,在字符串打印出來后換行。
以下的表格展示了 Java 轉(zhuǎn)義序列:
轉(zhuǎn)義序列 |
描述 |
---|---|
\t |
在文本中插入一個(gè)標(biāo)簽。 |
\b |
在文本中插入一個(gè)退格。 |
\n |
在文本中插入一個(gè)換行符。 |
\r |
在文本中插入一個(gè)回車。 |
\f |
在文本中插入一個(gè)換頁。 |
\' |
在文本中插入一個(gè)單引號字符。 |
\\ |
在文本中插入一個(gè)反斜杠字符。 |
當(dāng)一個(gè)轉(zhuǎn)義序列遇到一個(gè)打印語句,編譯器就會(huì)相應(yīng)地解譯。
示例
如果你想把引號放入引號內(nèi),必須使用轉(zhuǎn)義序列, \” ,在內(nèi)部引用:
1
2
3
4
5
6
|
public class Test { public static void main(String args[]) { System.out.println( "She said \"Hello!\" to me." ); } } |
這將產(chǎn)生以下結(jié)果:
She said "Hello!" to me.
Character 方法
以下列表是實(shí)現(xiàn) Character 類所有子類的重要的實(shí)例方法:
SN |
方法描述 |
---|---|
1 |
isLetter() |
2 |
isDigit() |
3 |
isWhitespace() |
4 |
isUpperCase() |
5 |
isLowerCase() |
6 |
toUpperCase() |
7 |
toLowerCase() |
8 |
toString() |
字符串
字符串,它被廣泛應(yīng)用于 Java 編程,是一個(gè)字符序列。在 Java 編程語言中,字符串是對象。
Java 平臺(tái)提供了 String 類來創(chuàng)建和操作字符串。
創(chuàng)建字符串
最直接的方式來創(chuàng)建一個(gè)字符串是這樣寫的:
String greeting = "Hello world!";
當(dāng)你創(chuàng)建一個(gè)字符串時(shí),編譯器在這種情況下用它的值創(chuàng)建一個(gè) String 對象,如:"Hello world!'。
任何其他對象可以通過使用 new 關(guān)鍵字,并通過構(gòu)造函數(shù)創(chuàng)建 String 對象。 String 類有11種構(gòu)造函數(shù)提供使用不同類型的字符串的初始值,如一個(gè)字符數(shù)組。
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 ); } } |
這將產(chǎn)生以下結(jié)果:
1
|
hello. |
注 String 類是不可變的,因此,一旦創(chuàng)建了 String 對象那么是不能改變的。如果需要大量修改字符的字符串,那么應(yīng)該使用 StringBuffer & StringBuilder 類。
String 長度
用于獲取有關(guān)對象的信息的方法稱為存取方法。可以和字符串一起使用的一個(gè)存取方法是 length() ,它返回包含在字符串對象中的字符數(shù)。
下面的兩行代碼被執(zhí)行之后,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 ); } } |
這將產(chǎn)生以下結(jié)果:
1
|
String Length is : 17 |
連接字符串
String類包括用于連接兩個(gè)字符串的方法:
1
|
string1.concat(string2); |
這返回一個(gè)新的字符串,即在 string1 結(jié)尾處添加 string2。還可以使用 concat()方法連接字符串,如:
1
|
"My name is " .concat( "Zara" ); |
字符串更常使用 “ + ” 運(yùn)算符連接在一起,如:
1
|
"Hello," + " world" + "!" |
這將產(chǎn)生:
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" ); } } |
這將產(chǎn)生以下結(jié)果:
1
|
Dot saw I was Tod |
創(chuàng)建格式化字符串
已經(jīng)有 printf() 和 format() 方法來打印輸出格式的數(shù)字。 String 類有一個(gè)等價(jià)的方法 format(),它返回一個(gè) String 對象,而不是一個(gè) PrintStream 對象。
使用字符串的靜態(tài) format() 方法允許創(chuàng)建可重復(fù)使用的格式化字符串,而不是一次性的 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) |