java.lang.String類常用方法


java.lang.String類
  1. public final class String extends Object implements Serializable,Comparable<String>,CharSequence
String類代表字符串
字符串是常量,他們的值在創建之后不能改變
String類包括的方法有:檢查序列的單個字符;比較字符串;搜索字符串;提取子字符串;創建字符串副本(在該副本中,所有的字符都被轉換為大寫或小寫形式)。
Java語言提供對字符串串聯符號(“+”)和其他對象到字符串的轉換的特殊支持。
字符串串聯是通過StringBuilder(或StringBuffer)類及其append方法實現的。
字符串轉換是通過toString方法實現的,該方法有Object類定義,並可被Java中所有類繼承
方法:
s.chartAt(int index)
返回指定索引處的char值。索引范圍:0-length()-1.
拋出:IndexOutBoundsException(如果index參數為負數或小於此字符串的長度)
  1. public static void main(String[] args) {
  2. String s = "wozhishishishizhegeyonfadaodihaobuhaoyong";
  3. System.out.println(s.charAt(2));
  4. }
  5. //運行結果:
  6. z
s.compareTo(String anotherString)
按字典順序比較兩個字符串。該比較基於字符串中各個字符的Unicode值。
將此String對象表示的字符序列與參數字符串所表示的字符序列進行比較。如果按照字典順序此String對象在參數字符串之前,則結果為一個負整數,在參數字符串之后,則比較結果為一個正整數。如果兩個字符串相等,則結果為0.compareTo只有在方法equals(Object)返回true時才會返回0.
如果這兩個字符串不同,要么他們在某個索引處有不同的字符,該索引對二者均為有效索引,要么他們的長度不同,或者同時具備上述兩者情況。
如果它們在一個或多個索引位置上具有不同的字符,假設k是這類索引的最小值,compareTo返回這兩個字符串在位置K處的兩個不同的char值:this.charAt(k)-anotherString.charAt(k)。如果它們沒有不同的索引位置,則較短字符串在字典順序上位於較長字符串的前面。這種情況下,compareTo返回這兩個字符串長度的不同:this.length()-anotherString.length()
s.compareToIgnoreCase(String str)
不考慮大小寫,安字段順序比較兩個字符串
s.concat(String str)
將指定字符串連到字符串的結尾
如果參數字符串的長度為0,則返回此String對象,否則,創建一個新的String對象。用來表示由此String對象表示的字符序列和有參數字符串表示的字符序列串聯而成的字符序列
  1. public static void main(String[] args) {
  2. String s = "wozhi";
  3. System.out.println(s.concat("nihaoya"));
  4. }
  5. 運行結果:
  6. wozhinihaoya
s.contains(CharSequence ss)
當且僅當此字符串包含char值的指定序列時,才返回true.
拋出:NullPointerException(如果ss為null)
  1. public static void main(String[] args) {
  2. String s = "wozhi";
  3. System.out.println(s.contains("o"));
  4. }
  5. 運行結果:
  6. true
s.contentEquals(CharSequence cs)
當且僅當此String表示與指定序列相同的char值的序列時;才返回true,否則返回false.
拋出:NullPointerException(如果cs為null)
  1. public static void main(String[] args) {
  2. String s = "abcdefg";
  3. System.out.println(s.contentEquals("abc"));
  4. System.out.println(s.contentEquals("efg"));
  5. System.out.println(s.contentEquals("abcdefg"));
  6. }
  7. 運行結果:
  8. false
  9. false
  10. true
 
copyValueOf(char[] data)
返回指定數組中表示該字符序列的字符串
copyValueOf(char[] data,int index,int count)
返回指定數組中表示該字符序列的字符串(index表示從第幾個字符開始,count表示總長度為多少)
  1. public static void main(String[] args) {
  2. char[] charArr = {'j','a','v','a'};
  3. String s = String.copyValueOf(charArr);
  4. String ss = String.copyValueOf(charArr,2,2);
  5. System.out.println(s);
  6. System.out.println(ss);
  7. }
  8. 運行結果:
  9. java
  10. va
endsWith(String suffix)
測試此字符串是否以指定的后綴結束
如果該參數是空字符串或等於有equals(object)方法確定的String對象,則結果為true
  1. public static void main(String[] args) {
  2. String s = "abcdefg";
  3. System.out.println(s.endsWith(""));
  4. System.out.println(s.endsWith("fg"));
  5. System.out.println(s.endsWith("abcdefg"));
  6. System.out.println(s.endsWith("g"));
  7. }
  8. 運行結果:
  9. true
  10. true
  11. true
  12. true
equals(String anObject)
比較此字符串與指定對象。當且僅當該參數不為null,並且是表示與此對象相同的字符序列的String對象,結果才為true
  1. public static void main(String[] args) {
  2. String s = "abcdefg";
  3. String ss = "";
  4. System.out.println(s.equals("abcdefg"));
  5. System.out.println(ss.equals(""));
  6. }
  7. 運行結果:
  8. true
  9. true
equalsIgnoreCase(String anotherString)
將兩個String進行比較(字符串長度相等,且相應的字符都相等),不考慮大小寫。
  1. public static void main(String[] args) {
  2. String s = "abcdefg";
  3. String ss = "ABCDEFG";
  4. System.out.println(s.equalsIgnoreCase(ss));
  5. }
  6. 運行結果:
  7. true
hashCode()
返回此字符串的哈希碼。String對象的哈希碼按下列公式計算:s[0]*31^(n-1)+s[1]*31^(n-2)+……+s[n-1].空字符串的哈希碼為0
  1. public static void main(String[] args) {
  2. String s = "abcdefg";
  3. System.out.println(s.hashCode());
  4. }
  5. 運行結果:
  6. -1206291356
indexOf(int ch)
返回指定字符在此字符串中第一次出現的索引,若未出現此字符,則返回-1
indexOf(int ch,int fromIndex)
從指定的索引開始搜索,返回在此字符串中第一次出現指定字符處的索引
  1. public static void main(String[] args) {
  2. String s = "abcdefg";
  3. System.out.println(s.indexOf("a"));
  4. System.out.println(s.indexOf("cde"));
  5. System.out.println(s.indexOf("o"));
  6. }
  7. 運行結果:
  8. 0
  9. 2
  10. -1
  1. public static void main(String[] args) {
  2. String s = "abcadefg";
  3. System.out.println(s.indexOf("a",2));
  4. }
  5. 運行結果:
  6. 3
lastIndexOf(String str,int formIndex)
從指定的索引處開始向后搜索,返回在此字符串中最后一次出現的指定子字符串的索引
lastIndexOf(String str)
飯后在此字符串中最右邊出現的指定子字符串的索引
  1. public static void main(String[] args) {
  2. String s = "abcadefg";
  3. System.out.println(s.lastIndexOf("a"));
  4. System.out.println(s.lastIndexOf("a",2));
  5. }
  6. 運行結果:
  7. 3
  8. 0
length()
返回此字符串的長度
matches(String regex)
通知此字符串食肉匹配給定的正則表達式
replace(char oldChar,char newChar)
返回一個新的字符串,它是通過用newChar替換此字符串中出現的所有oldChar而生成的
replace(CharSequence target,CharSequence replacement)
使用指定的字面值替換序列替換此字符串匹配字面值目標序列的每個子字符串
replaceAll(String regex,String replacement)
使用指定的字面值替換此字符串匹配字面值目標序列的每個字子字符串
replaceFirst(String regex,String replacement)
使用給定的replacement字符串替換此字符串匹配給定的正則表達式的第一個子字符串
split(String regex)
根據匹配給定的正則表達式來拆分此字符串
startWith(String prefix)
測試此字符串是否以指定的前綴開始
substring(int beginIndex)
返回一個新的字符串,它是此字符串的一個子字符串。該子字符串始於指定索引處的字符,一直到此字符串末尾
substring(int beginIndex.int endIndex)
返回一個新的字符串,始於指定索引beginIndex,終於指定索引endIndex的字符
  1. public static void main(String[] args) {
  2. String s = "abcadefg";
  3. System.out.println(s.substring(2));
  4. System.out.println(s.substring(2,5));
  5. }
  6. 運行結果:
  7. cadefg
  8. cad
toCharArray()
將此字符串轉換為一個新的字符數組
  1. public static void main(String[] args) {
  2. String s = "abcadefg";
  3. char[] ss = s.toCharArray();
  4. System.out.println(ss);
  5. }
  6. 運行結果:
  7. abcadefg
toLowerCase()
使用默認語言環境的規則將此String中的所有字符都轉換成小寫
toUpperCase()
使用默認語言環境的規則將此String中的所有字符都轉換為大寫
  1. public static void main(String[] args) {
  2. String s = "abcadefg";
  3. String ss = "ABCDEFG";
  4. System.out.println(s.toUpperCase());
  5. System.out.println(ss.toLowerCase());
  6. }
  7. 運行結果:
  8. ABCADEFG
  9. abcdefg
valueOf(boolean b)
返回Boolean參數額字符串表示形式
valueOf(char c)
返回char參數的字符串表示形式
valueOf(int i)
返回int參數的字符串表現形式;long、float、double同樣
  1. public static void main(String[] args) {
  2. char s = 'q';
  3. System.out.println(String.valueOf(false));
  4. System.out.println(String.valueOf(s));
  5. System.out.println(String.valueOf(2.34));
  6. System.out.println(String.valueOf(123456));
  7. System.out.println(String.valueOf(1));
  8. }
  9. 運行結果:
  10. false
  11. q
  12. 2.34
  13. 123456
  14. 1


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM