Java中的字符串
以下內容引用自http://wiki.jikexueyuan.com/project/java/strings.html:
字符串,它被廣泛應用於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類支持的方法列表:
方法 | 描述 |
---|---|
char charAt(int index) | 返回指定索引處的字符。 |
int compareTo(Object o) | 將這個字符串與另一個對象比較。 |
int compareTo(String anotherString) | 比較兩個字符串的字典順序。 |
int compareToIgnoreCase(String str) | 比較兩個字符串按字典順序,不區分大小寫的差異。 |
String concat(String str) | 將指定的字符串串連到這個字符串的結尾。 |
boolean contentEquals(StringBuffer sb) | 返回true當且僅當該字符串代表相同的字符序列作為指定的StringBuffer。 |
static String copyValueOf(char[] data) | 返回表示所指定的數組中的字符序列的字符串。 |
static String copyValueOf(char[] data, int offset, int count) | 返回表示所指定的數組中的字符序列的字符串。 |
boolean endsWith(String suffix) | 測試此字符串是否以指定的後綴結束。 |
boolean equals(Object anObject) | 比較此字符串與指定的對象。 |
boolean equalsIgnoreCase(String anotherString) | 比較這個字符串到另一個字符串,忽略大小寫的考慮。 |
byte getBytes() | 將此String解碼使用平臺的默認字符集,並將結果存儲到一個新的字節數組中的字節序列。 |
byte[] getBytes(String charsetName | 將此String解碼使用指定的字符集的字節序列,並將結果存儲到一個新的字節數組。 |
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) | 從這個字符串復制字符到目標字符數組。 |
int hashCode() | 為這個字符串返回一個哈希代碼。 |
int indexOf(int ch) | 返回此字符串指定字符第一次出現處的索引。 |
int indexOf(int ch, int fromIndex) | 返回索引這個字符串中指定字符第一次出現處,指定索引處開始搜索。 |
int indexOf(String str) | 返回此字符串指定子字符串的第一次出現處的索引。 |
int indexOf(String str,int fromIndex) | 返回這個字符串中指定子字符串的第一次出現處的索引,從指定的索引處開始。 |
String intern() | 返回字符串對象的規範化表示。 |
int lastIndexOf(int ch) | 返回此字符串指定字符最後一次出現處的索引。 |
int lastIndexOf(int ch, int fromIndex) | 返回此字符串指定字符最後一次出現處的索引,從指定索引開始向後搜索。 |
int lastIndexOf(String str) | 返回此字符串指定子字符串的最右邊出現處的索引。 |
int lastIndexOf(String str, int fromIndex) | 返回索引這個字符串中指定子字符串的最後出現處,從指定的索引開始處向後搜索。 |
int length() | 返回此字符串的長度。 |
boolean matches(String regex) | 判斷此字符串是否與給定的正則表達式匹配。 |
boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) | 測試兩個字符串的區域等於。 |
boolean regionMatches(int toffset, String other, int ooffset, int len) | 測試兩個字符串的區域都是相等的。 |
String replace(char oldChar, char newChar) | 返回從newChar更換oldChar所有出現在此字符串中產生一個新的字符串。 |
String replaceAll(String regex, String replacement) | 替換此字符串中給定的正則表達式與給定替換相匹配的每個子字符串。 |
String replaceFirst(String regex, String replacement) | 替換此字符串匹配給定的正則表達式給定替換第一個子字符串。 |
String[] split(String regex) | 分割圍繞給定的正則表達式匹配的這個字符串。 |
String[] split(String regex, int limit) | 分割圍繞給定的正則表達式匹配的這個字符串。 |
boolean startsWith(String prefix) | 測試此字符串是否以指定的前綴開頭。 |
boolean startsWith(String prefix, int toffset) | 測試此字符串是否以指定索引開始的指定前綴開始。 |
CharSequence subSequence(int beginIndex, int endIndex) | 返回一個新的字符序列,這個序列的子序列。 |
String substring(int beginIndex) | 返回一個新的字符串,它是此字符串的一個子字符串。 |
String substring(int beginIndex, int endIndex) | 返回一個新的字符串,它是此字符串的一個子字符串。 |
char[] toCharArray() | 這個字符串轉換為一個新的字符數組。 |
String toLowerCase() | 將所有在此字符串中的字符使用默認語言環境的規則小寫。 |
String toLowerCase(Locale locale) | 將所有在此字符串中的字符使用給定Locale的規則小寫。 |
String toString() | 這個對象(它已經是一個字符串!)返回字符串形式(這裏是自己本身)。 |
String toUpperCase() | 使用默認語言環境的規則將此String中所有的字符轉換為大寫。 |
String toUpperCase(Locale locale) | 使用給定Locale的規則將此String中所有的字符轉換為大寫。 |
String trim() | 返回字符串的一個副本,開頭和結尾的空格去除。 |
static String valueOf(primitive data type x) | 返回傳遞的數據類型參數的字符串表示形式。 |
測試工程:https://github.com/easonjim/5_java_example/tree/master/javabasicstest/test9
Java中的字符串