Java的String和StringBuilder


一.String

  1.創建String對象的方法:

    String s1="haha";

    String s2=new String();

    String s3=new String("haha");

  2.String常用方法:

 char charAt(int index) 
          返回指定索引處的 char 值。
 int codePointAt(int index) 
          返回指定索引處的字符(Unicode 代碼點)。
 int codePointBefore(int index) 
          返回指定索引之前的字符(Unicode 代碼點)。
 int codePointCount(int beginIndex, int endIndex) 
          返回此 String 的指定文本范圍中的 Unicode 代碼點數。
 int compareTo(String anotherString) 
          按字典順序比較兩個字符串。
 int compareToIgnoreCase(String str) 
          按字典順序比較兩個字符串,不考慮大小寫。
 String concat(String str) 
          將指定字符串連接到此字符串的結尾。
 boolean contains(CharSequence s) 
          當且僅當此字符串包含指定的 char 值序列時,返回 true。
 boolean contentEquals(CharSequence cs) 
          將此字符串與指定的 CharSequence 比較。
 boolean contentEquals(StringBuffer sb) 
          將此字符串與指定的 StringBuffer 比較。
static String copyValueOf(char[] data) 
          返回指定數組中表示該字符序列的 String。
static String copyValueOf(char[] data, int offset, int count) 
          返回指定數組中表示該字符序列的 String。
 boolean endsWith(String suffix) 
          測試此字符串是否以指定的后綴結束。
 boolean equals(Object anObject) 
          將此字符串與指定的對象比較。
 boolean equalsIgnoreCase(String anotherString) 
          將此 String 與另一個 String 比較,不考慮大小寫。
static String format(Locale l, String format, Object... args) 
          使用指定的語言環境、格式字符串和參數返回一個格式化字符串。
static String format(String format, Object... args) 
          使用指定的格式字符串和參數返回一個格式化字符串。
 byte[] getBytes() 
          使用平台的默認字符集將此 String 編碼為 byte 序列,並將結果存儲到一個新的 byte 數組中。
 byte[] getBytes(Charset charset) 
          使用給定的 charset 將此 String 編碼到 byte 序列,並將結果存儲到新的 byte 數組。
 void getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin) 
          已過時。 該方法無法將字符正確轉換為字節。從 JDK 1.1 起,完成該轉換的首選方法是通過 getBytes() 方法,該方法使用平台的默認字符集。
 byte[] getBytes(String charsetName) 
          使用指定的字符集將此 String 編碼為 byte 序列,並將結果存儲到一個新的 byte 數組中。
 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() 
          返回字符串對象的規范化表示形式。
 boolean isEmpty() 
          當且僅當 length() 為 0 時返回 true
 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) 
          告知此字符串是否匹配給定的正則表達式。
 int offsetByCodePoints(int index, int codePointOffset) 
          返回此 String 中從給定的 index 處偏移 codePointOffset 個代碼點的索引。
 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 replace(CharSequence target, CharSequence replacement) 
          使用指定的字面值替換序列替換此字符串所有匹配字面值目標序列的子字符串。
 String replaceAll(String regex, String replacement) 
          使用給定的 replacement 替換此字符串所有匹配給定的正則表達式的子字符串。
 String replaceFirst(String regex, String replacement) 
          使用給定的 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 中的所有字符都轉換為小寫。
 String toLowerCase(Locale locale) 
          使用給定 Locale 的規則將此 String 中的所有字符都轉換為小寫。
 String toString() 
          返回此對象本身(它已經是一個字符串!)。
 String toUpperCase() 
          使用默認語言環境的規則將此 String 中的所有字符都轉換為大寫。
 String toUpperCase(Locale locale) 
          使用給定 Locale 的規則將此 String 中的所有字符都轉換為大寫。
 String trim() 
          返回字符串的副本,忽略前導空白和尾部空白。
static String valueOf(boolean b) 
          返回 boolean 參數的字符串表示形式。
 1 package Strings;
 2 
 3 public class StringTest1 {
 4 
 5     public static void main(String[] args) {
 6 
 7         String str="I am a student!";
 8         //求字符串長度
 9         System.out.println("str長度為:"+str.length());
10         //求某個位置的字符
11         System.out.println("位置為7的字符為:"+str.charAt(7));
12         //求子字符串
13         System.out.println("第7到15個字符組成的子字符串為:"+str.substring(7, 14));
14         System.out.println("第7到15個字符組成的子字符串為:"+str.substring(7));
15         //求字符在字符串中第一次出現的位置
16         System.out.println("第一次出現a的位置:"+str.indexOf("a"));
17         //求字串am在字符串中第一次出現的位置
18         System.out.println("第一次出現am的位置:"+str.indexOf("am"));
19 
20         //求字符在字符串中最后一次出現的位置
21         System.out.println("最后次出現a的位置:"+str.lastIndexOf("a"));
22         //求字串am在字符串中最后一次出現的位置
23         System.out.println("最后一次出現am的位置:"+str.indexOf("am"));
24         //。。。。。。。。
25 
26 
27     }
28 
29 }
View Code

   3.equals和==的區別:

    equals是判斷內容是不是相同,==是判斷內存地址是不是相同的。(相當於Python中的==【equals】和is【is】)。

 1 package Strings;
 2 
 3 public class StringTEst3 {
 4 
 5     public static void main(String[] args) {
 6 
 7         String str1="hello";
 8         String str2="hello";
 9         String str3=new String("hello");
10         System.out.println("str1和str2的內容相等:"+(str1.equals(str2)));
11         System.out.println("str1和str3的內容相等:"+(str1.equals(str3)));
12         /*在str1創建時,會在棧中生成一個變量str1,並在常量池中生成"hello"並將str1指向"hello"
13             ,然后當str2創建時,會在棧中生成str2,然后發現“hello”已經存在變量池中,就會把str2也指向hello
14             所以str1和str2的地址是一樣的
15         */
16         System.out.println("str1和str2的內存地址相等:"+(str1==str2));
17         System.out.println("str1和str3的內存地址相等:"+(str1==str3));
18 
19     }
20 
21 }
22 /*
23  * 輸出:
24  *     str1和str2的內容相等:true
25     str1和str3的內容相等:true
26     str1和str2的內存地址相等:true
27     str1和str3的內存地址相等:false
28     */
View Code

   4.字符串的不可變性:

    String對象一旦創建,則不能修改,是不可變的,所謂的修改其實是創建了新的對象,所指向的空間不變。

 1 package Strings;
 2 
 3 public class StringTest4 {
 4     public static void main(String[] args){
 5         String str1="hello";
 6         str1=str1+",haha";
 7         //創建了新的對象,str1不在指向"hello",而是指向"hello,haha"
 8         System.out.println(str1);
 9     }
10 
11 }
View Code

 二.StringBuilder

  1.String和StringBuilder的區別:

    String具有不可變性,而StringBuilder不具備。當頻繁操作字符串時使用StringBuilder。

  2.StringBuilder和StringBuffer:

    兩者基本相似,但StringBuffer是線程安全的,StringBuilder則沒有,所以性能略高(推薦使用,因為大多數情況下處理字符串都是單線程的)。

  3.StringBuilder:

構造方法摘要
StringBuilder() 
          構造一個不帶任何字符的字符串生成器,其初始容量為 16 個字符。
StringBuilder(CharSequence seq) 
          構造一個字符串生成器,它包含與指定的 CharSequence 相同的字符。
StringBuilder(int capacity) 
          構造一個不帶任何字符的字符串生成器,其初始容量由 capacity 參數指定。
StringBuilder(String str) 
          構造一個字符串生成器,並初始化為指定的字符串內容。

 

方法摘要
 StringBuilder append(boolean b) 
          將 boolean 參數的字符串表示形式追加到序列。
 StringBuilder append(char c) 
          將 char 參數的字符串表示形式追加到此序列。
 StringBuilder append(char[] str) 
          將 char 數組參數的字符串表示形式追加到此序列。
 StringBuilder append(char[] str, int offset, int len) 
          將 char 數組參數的子數組的字符串表示形式追加到此序列。
 StringBuilder append(CharSequence s) 
          向此 Appendable 追加到指定的字符序列。
 StringBuilder append(CharSequence s, int start, int end) 
          將指定 CharSequence 的子序列追加到此序列。
 StringBuilder append(double d) 
          將 double 參數的字符串表示形式追加到此序列。
 StringBuilder append(float f) 
          將 float 參數的字符串表示形式追加到此序列。
 StringBuilder append(int i) 
          將 int 參數的字符串表示形式追加到此序列。
 StringBuilder append(long lng) 
          將 long 參數的字符串表示形式追加到此序列。
 StringBuilder append(Object obj) 
          追加 Object 參數的字符串表示形式。
 StringBuilder append(String str) 
          將指定的字符串追加到此字符序列。
 StringBuilder append(StringBuffer sb) 
          將指定的 StringBuffer 追加到此序列。
 StringBuilder appendCodePoint(int codePoint) 
          將 codePoint 參數的字符串表示形式追加到此序列。
 int capacity() 
          返回當前容量。
 char charAt(int index) 
          返回此序列中指定索引處的 char 值。
 int codePointAt(int index) 
          返回指定索引處的字符(統一代碼點)。
 int codePointBefore(int index) 
          返回指定索引前的字符(統一代碼點)。
 int codePointCount(int beginIndex, int endIndex) 
          返回此序列指定文本范圍內的統一代碼點。
 StringBuilder delete(int start, int end) 
          移除此序列的子字符串中的字符。
 StringBuilder deleteCharAt(int index) 
          移除此序列指定位置上的 char
 void ensureCapacity(int minimumCapacity) 
          確保容量至少等於指定的最小值。
 void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 
          將字符從此序列復制到目標字符數組 dst
 int indexOf(String str) 
          返回第一次出現的指定子字符串在該字符串中的索引。
 int indexOf(String str, int fromIndex) 
          從指定的索引處開始,返回第一次出現的指定子字符串在該字符串中的索引。
 StringBuilder insert(int offset, boolean b) 
          將 boolean 參數的字符串表示形式插入此序列中。
 StringBuilder insert(int offset, char c) 
          將 char 參數的字符串表示形式插入此序列中。
 StringBuilder insert(int offset, char[] str) 
          將 char 數組參數的字符串表示形式插入此序列中。
 StringBuilder insert(int index, char[] str, int offset, int len) 
          將數組參數 str 子數組的字符串表示形式插入此序列中。
 StringBuilder insert(int dstOffset, CharSequence s) 
          將指定 CharSequence 插入此序列中。
 StringBuilder insert(int dstOffset, CharSequence s, int start, int end) 
          將指定 CharSequence 的子序列插入此序列中。
 StringBuilder insert(int offset, double d) 
          將 double 參數的字符串表示形式插入此序列中。
 StringBuilder insert(int offset, float f) 
          將 float 參數的字符串表示形式插入此序列中。
 StringBuilder insert(int offset, int i) 
          將 int 參數的字符串表示形式插入此序列中。
 StringBuilder insert(int offset, long l) 
          將 long 參數的字符串表示形式插入此序列中。
 StringBuilder insert(int offset, Object obj) 
          將 Object 參數的字符串表示形式插入此字符序列中。
 StringBuilder insert(int offset, String str) 
          將字符串插入此字符序列中。
 int lastIndexOf(String str) 
          返回最右邊出現的指定子字符串在此字符串中的索引。
 int lastIndexOf(String str, int fromIndex) 
          返回最后一次出現的指定子字符串在此字符串中的索引。
 int length() 
          返回長度(字符數)。
 int offsetByCodePoints(int index, int codePointOffset) 
          返回此序列中的一個索引,該索引是從給定 index 偏移 codePointOffset 個代碼點后得到的。
 StringBuilder replace(int start, int end, String str) 
          使用給定 String 中的字符替換此序列的子字符串中的字符。
 StringBuilder reverse() 
          將此字符序列用其反轉形式取代。
 void setCharAt(int index, char ch) 
          將給定索引處的字符設置為 ch
 void setLength(int newLength) 
          設置字符序列的長度。
 CharSequence subSequence(int start, int end) 
          返回一個新字符序列,該字符序列是此序列的子序列。
 String substring(int start) 
          返回一個新的 String,它包含此字符序列當前所包含字符的子序列。
 String substring(int start, int end) 
          返回一個新的 String,它包含此序列當前所包含字符的子序列。
 String toString() 
          返回此序列中數據的字符串表示形式。
 void trimToSize() 
          嘗試減少用於字符序列的存儲空間。

 

 1 package Strings;
 2 
 3 public class StringTest5 {
 4 
 5     public static void main(String[] args) {
 6         StringBuilder str=new StringBuilder("hello");
 7 //        str.append(",");
 8 //        str.append("world!");
 9 //        System.out.println("str="+str);
10         //添加字符串
11         System.out.println("str="+str.append(",").append("world!"));
12         //替換字符串
13         //1.先delete,后insert
14         System.out.println("str="+str.delete(6, 11).insert(6,"WORLD"));
15         //2.使用replece
16         System.out.println("str="+str.replace(6,11,"WORLD"));
17         //取出子字符串
18         System.out.println("str="+str.substring(0,2));
19 
20 
21     }
22 
23 }
View Code

 


免責聲明!

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



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