1.1、String
String 類是final修飾的,是頂級類,不可被繼承
String
類代表字符串。java 程序中的所有字符串字面值(如 "abc"
)都作為此類的實例實現。
字符串是常量;它們的值在創建之后不能更改
String str="abc";
String
類包括的方法可用于檢查序列的單個字符、比較字符串、搜索字符串、提取子字符串、創建字符串副本并將所有字符全部轉換為大寫或小寫。
1.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 參數的字符串表示形式。 |
static String | valueOf(char c) 返回 char 參數的字符串表示形式。 |
static String | valueOf(char[] data) 返回 char 數組參數的字符串表示形式。 |
static String | valueOf(char[] data, int offset, int count) 返回 char 數組參數的特定子數組的字符串表示形式。 |
static String | valueOf(double d) 返回 double 參數的字符串表示形式。 |
static String | valueOf(float f) 返回 float 參數的字符串表示形式。 |
static String | valueOf(int i) 返回 int 參數的字符串表示形式。 |
static String | valueOf(long l) 返回 long 參數的字符串表示形式。 |
static String | valueOf(Object obj) 返回 Object 參數的字符串表示形式。 |
1.3、String str="abc"與String str1=new String("abc")區別
package com.pb.string.demo1;public class Demo1 { public static void main(String[] args) { /* * s1一個字符串類類型變量,abc是一個對象 * 一旦被初始化,就不可以被改變 */ String str1="abc"; /* * new 就是在堆內存中再開辟一個新的空間 * s1只有一個對象abc * s2有2個對象abc和new String("abc"); */ String str2=new String("abc"); System.out.println(str1==str2);//false ==比較內存地址 /* * String 類重寫了Object中的equals方法,用來比較字符串內容是否相同 */ System.out.println(str1.equals(str2));//true 比較地址中的值 }}
1.4、獲取和判斷
獲取
獲取字符串長度length();
根據位置獲取位置下的字符char charAt(int index);
根據字符獲取該字字符串中位置 int indexOf(in ch)返回第一次出現的位置
從fromIndex(int ch,int fromIndex):獲取ch在字符串中出現位置
從fromIndex(String str,int fromIndex):獲取str在字符串中出現位置
package com.pb.string.demo1;public class StringDemo1 { public static void main(String[] args) { String str="abcdefghijklmnab"; //長度16 //長度 sop(str.length()); //根據索引獲取字符 sop(str.charAt(5)); //sop(str.charAt(33));//下標越界 //根據字符獲取索引 sop(str.indexOf('b')); //從0開始查找 //從指字位置開始 sop(str.indexOf('b',5));//從索引為5的字符形開始查找 //查找沒的字符 sop(str.indexOf('v',5));//-1 如果沒有找到返回-1 //從后向前查找 sop(str.lastIndexOf('b')); sop(str.lastIndexOf("b")); // } public static void sop(Object obj){ System.out.println(obj); }}
判斷
字符串中是否包含某一個子串 boolean contains(String str); indexOf(str) 可以索引str第一次出現的位置,如果返回-1,表示不包含在字符串,也可以用于指定判斷是否包含
字符串中是否有內容boolean isEmpty(),判斷長度是否為0,
字符串是否以指字內容開頭boolean startWith(String str)
字符串是否以指字內容結尾boolean endWith(String str)
判斷字符內容是否相同equals(String str),重寫Object中equals方法
判斷字符內容是否相同忽略大小寫equalsIgnoreCase(String str),
package com.pb.string.demo1;public class StringDemo2 { public static void main(String[] args) { String str="ArrayDemo.java"; sop(str.startsWith("Array")); //判斷是否以Array開頭 true sop(str.endsWith(".java"));//判斷是否以.java結尾 true sop(str.contains("Demo"));//判斷是否有(包含)Demo子串 true sop(str.equals("ArrayDemo.java"));//判斷字符串內容是否相同 true sop(str.equalsIgnoreCase("ArrayDemo.java"));//判斷字符串內容是否相同并忽略大小字書 true } public static void sop(Object obj){ System.out.println(obj); }}
1.5、轉換
將字符數組轉成字符串--構造方法
package com.pb.string.demo1;public class StringDemo3 { public static void main(String[] args) { char [] ch={'a','b','c','d','e','f','g'}; //將字符數組轉換成字符串 String str1=new String(ch); sop(str1); String str2=new String(ch,1,4);//第下標為1開始轉換4個字符s sop(str2); //靜態方法 String str3=String.copyValueOf(ch); sop(str3); String str4=String.copyValueOf(ch,1,5); sop(str4); String str5=String.valueOf(ch); sop(str5); String str6=String.valueOf(ch,1,4); sop(str6); } public static void sop(Object obj){ System.out.println(obj); }}
將字符串,轉換成字符數組
將字節數組轉換成字符串
char[] | toCharArray() 將此字符串轉換為一個新的字符數組 |
將字符串轉換成字節數組
package com.pb.string.demo1;public class StringDemo4 { public static void main(String[] args) { String str="abcdefg"; //將字符串轉換為字符數組 char [] ch=str.toCharArray(); //遍歷 for (int i = 0; i < ch.length; i++) { System.out.print(ch[i]+","); } //將字符串轉換為字節數組 byte [] b=str.getBytes(); for (int i = 0; i < b.length; i++) { System.out.print((char)b[i]); } } }
將基本數組類型轉換為字符串
package com.pb.string.demo1;public class StringDemo5 { public static void main(String[] args) { int i=3; //可以是double,float,short,long,char,byte String str1=i+""; String str2=String.valueOf(i); }}
1.6、切割和替換,截取
分離
package com.pb.string.demo1;import java.util.Arrays;public class SplitString { public static void main(String[] args) { String str1="zhangsa,lisi,wangwu,zhaoliu"; String[] arr=str1.split(","); System.out.println(Arrays.toString(arr)); }}
結果:[zhangsa, lisi, wangwu, zhaoliu]
替換
package com.pb.string.demo1;public class ReplaceDemo { public static void main(String[] args) { String str1="Hello,Java!"; String str2=str1.replace('a', 'n'); String str3=str1.replace('q', 'n');//如果替換的不在,返回的還是原來的 String str4=str1.replace("Java", "World"); System.out.println(str1); System.out.println(str2); System.out.println(str3); System.out.println(str4); }}
Hello,Java!
Hello,Jnvn!
Hello,Java!
Hello,World!
截取
package com.pb.string.demo1;public class SubStringDemo6 { public static void main(String[] args) { String s="abcdef"; System.out.println(s.substring(2));//cdef,從第二開始到結尾 System.out.println(s.substring(2, 4));//cd 從第二個到第4個但不包含第4個 System.out.println(s.substring(0,s.length())); //獲取全部 System.out.println(s.substring(0)); }}
1.7、比較和去除空格
比較
如果參數字符串等于此字符串,則返回值 0
;如果此字符串按字典順序小于字符串參數,則返回一個小于 0
的值;如果此字符串按字典順序大于字符串參數,則返回一個大于 0
的值。
去空格
將字符串轉換為大寫或者小寫
package com.pb.string.demo1;public class StringDemo6 { public static void main(String[] args) { String s=" Hello Java! "; //轉換為小寫 System.out.println(s.toLowerCase()); //轉換為大寫 System.out.println(s.toUpperCase()); //去除2邊的空格 System.out.println(s.trim()); }}
結果:
hello java! HELLO JAVA! Hello Java!
1.8、去掉字符串兩邊的空格
package com.pb.string.demo2;/** * 模擬一個trim方法,去除字符串2邊的空格 * @author Denny * 判斷字符串開始第一個位置是不是空格,是斷續直到不是空格 為步 * 結尾處判斷空格一樣 * 當開始和結尾都不是空格時,就是要獲取的字符串 * * */public class StringDemo1 { public static void main(String[] args) { String str=" Hello Java! "; sop(str);//去空格前 str=myTrim(str); sop(str);//去空格后 } public static String myTrim(String str){ int start=0,end=str.length()-1; //開始的空格 while(start<=end&&str.charAt(start)==' '){ start++; } //結尾的空格 while(start<=end&&str.charAt(end)==' '){ end--; } return str.substring(start,end+1);//截取 } public static void sop(Object obj){ System.out.println(obj); }}
Hello Java! Hello Java!
1.9、字符串反轉
package com.pb.string.demo2;/** * 將一個字符串返轉 * @author denny *思路: *可以將字符串變成數組,對數組反轉 *將反轉后的數組,轉換成字符串 *只要將或者反轉的部分的開始和結束位置作為參數傳遞即可 */public class StringDemo2 { public static void main(String[] args) { String str="abcdef"; sop(str); str=reverseString(str,1,5); System.out.println(str); } //從某個位置開始反轉到某個位置結束 public static String reverseString(String s,int start,int end){ char [] ch=s.toCharArray(); reverse(ch,start,end); return new String(ch); } //方法重載,反轉全部 public static String reverseString(String s){ return reverseString(s,0,s.length()); } //反轉 參數,開始位到結束位置,但不包含結束位 private static void reverse(char [] arr,int x,int y){ for(int start=x,end=y-1;start<end;start++,end--){ swap(arr,start,end); //交換值 } } //交換 private static void swap(char [] arr,int start,int end){ char tmp=arr[start]; arr[start]=arr[end]; arr[end]=tmp; } public static void sop(Object obj){ System.out.println(obj); }}
abcdefaedcbf
1.10、獲取字符串在另一個字符串中出現的次數
package com.pb.string.demo2;/** * 獲取字符串在另一個字符串中出現的次數 * * @author Denny 定義一個計數器 獲取第一次出現的位置 從第一次出現的位置后,剩余的字符串中繼續獲取再現的位置 每獲取一次就加1 * 當獲取不到時計數完成 */public class StringDemo3 { public static void main(String[] args) { String s1 = "kkabkkcdkkefkkskk"; String s2 = "kk"; int count = getCount_2(s1, s2); System.out.println("count="+count); } public static int getCount(String s1, String key) { int count = 0; int index=0;//下標 while((index=s1.indexOf(key))!=-1){ sop("index="+index); sop("s1="+s1); s1=s1.substring(index+key.length()); //截取字符串,再賦值給字符串 count++; } return count; } public static int getCount_2(String s1, String key) { int count = 0; int index=0;//下標 while((index=s1.indexOf(key,index))!=-1){ index=index+key.length(); sop("index="+index); count++; } return count; } public static void sop(Object obj){ System.out.println(obj); }}
結果:
index=2index=6index=10index=14index=17count=5
1.11、獲取2個字符串最大相同的串
package com.pb.string.demo2;/** * * @author Denny 獲取2個字符串最大相同的串, 將短的字符進行一長度遞減的子串打印 將知的字符串按照長度弟減的方式獲取到 如果包含就找到! */public class StringDemo4 { public static void main(String[] args) { String s1 = "abcwerthelloyuiodef"; String s2 = "cvhelloybnm"; sop("最大相同的是:"+getMaxString(s2,s1)); } public static String getMaxString(String s1, String s2) { String max = "", min = ""; //判斷哪個字符串長 max = (s1.length() > s2.length()) ? s1 : s2; min=(max==s1)?s2:s1; //System.out.println("max="+max+",min="+min); //使用小的字符串做循環 for (int i = 0; i < min.length(); i++) { //每次都從0開始,從后減少 for(int y=0,z=min.length()-i;z!=min.length()+1;y++,z++){ String temp=min.substring(y,z); sop(temp); if(max.contains(temp)){ return temp; } } } return ""; } public static void sop(Object obj){ System.out.println(obj); }}
cvhelloybnmcvhelloybnvhelloybnmcvhelloybvhelloybnhelloybnmcvhelloyvhelloybhelloybnelloybnmcvhellovhelloyhelloybelloybnlloybnmcvhellvhellohelloy最大相同的是:helloy
2.1、StringBuffer
首先StringBuffer是字符串緩沖區,是一個容器。
每個字符串緩沖區都有一定的容量。只要字符串緩沖區所包含的字符序列的長度沒有超出此容量,就無需分配新的內部緩沖區數組。如果內部緩沖區溢出,則此容量自動增大。從 JDK 5 開始,為該類補充了一個單個線程使用的等價類,即 StringBuilder
。與該類相比,通常應該優先使用 StringBuilder 類,因為它支持所有相同的操作,但由于它不執行同步,所以速度更快。
1.長度是可以變化的
2.可以操作多個數據類型
3.最終會通過toString()方法變成字符串.
容器:增加,刪除,改,查
c----create append()將指定字符串,添加到現在的字符串末尾
u----update
r-----read
d----delete
append()將指定字符串,添加到現在的字符串末尾
insert插入到指定位置append(索引,要插入的字符串或者其它類型)
delete 刪除
2.2、示例
添加,插入
package com.pb.stringbuffer.demo1;public class StringBufferDemo1 { public static void main(String[] args) { StringBuffer sb=new StringBuffer(); sb.append("abcd").append(123).append("mmmmmmmm"); //插入 sb.insert(2, "QQqqqqqqqqq"); System.out.println(sb); }}
刪除 包含start不包含end
StringBuffer | delete(int start, int end) 移除此序列的子字符串中的字符。 |
StringBuffer | deleteCharAt(int index) 移除此序列指定位置的 char 。 |
package com.pb.stringbuffer.demo1;public class StringBufferDemo2 { public static void main(String[] args) { getChar(); } //獲取指定字符串,并存放在字符數組中 public static void getChar(){ StringBuffer sb=new StringBuffer("abcdef"); char [] ch=new char[8]; sb.getChars(1, 4, ch, 2); for (int i = 0; i < ch.length; i++) { sop("ch["+i+"]="+ch[i]); } } //修改 public static void update(){ StringBuffer sb=new StringBuffer("abcdef"); sb.replace(1, 4, "33333333333"); sop(sb); //替換一個 sb.setCharAt(1, 'k'); sop(sb); } public static void del(){ StringBuffer sb=new StringBuffer("abcdefghi"); sb.delete(1, 3); sop(sb); //刪除一個 sb.deleteCharAt(sb.indexOf("i")); sop(sb); //清空緩沖區 sb.delete(0, sb.length()); sop(sb); } //添加,插入 public static void add_insert(){ StringBuffer sb=new StringBuffer(); sb.append("abcd").append(123).append("mmmmmmmm"); //插入 sb.insert(2, "qqqqqqqqqqq"); System.out.println(sb); } public static void sop(Object obj){ System.out.println(obj); }}
2.3、常用方法
構造方法摘要 | |
---|---|
StringBuffer() 構造一個其中不帶字符的字符串緩沖區,其初始容量為 16 個字符。 | |
StringBuffer(CharSequence seq) public java.lang.StringBuilder(CharSequence seq) 構造一個字符串緩沖區,它包含與指定的 CharSequence 相同的字符。 | |
StringBuffer(int capacity) 構造一個不帶字符,但具有指定初始容量的字符串緩沖區。 | |
StringBuffer(String str) 構造一個字符串緩沖區,并將其內容初始化為指定的字符串內容。 |
方法摘要 | |
---|---|
StringBuffer | append(boolean b) 將 boolean 參數的字符串表示形式追加到序列。 |
StringBuffer | append(char c) 將 char 參數的字符串表示形式追加到此序列。 |
StringBuffer | append(char[] str) 將 char 數組參數的字符串表示形式追加到此序列。 |
StringBuffer | append(char[] str, int offset, int len) 將 char 數組參數的子數組的字符串表示形式追加到此序列。 |
StringBuffer | append(CharSequence s) 將指定的 CharSequence 追加到該序列。 |
StringBuffer | append(CharSequence s, int start, int end) 將指定 CharSequence 的子序列追加到此序列。 |
StringBuffer | append(double d) 將 double 參數的字符串表示形式追加到此序列。 |
StringBuffer | append(float f) 將 float 參數的字符串表示形式追加到此序列。 |
StringBuffer | append(int i) 將 int 參數的字符串表示形式追加到此序列。 |
StringBuffer | append(long lng) 將 long 參數的字符串表示形式追加到此序列。 |
StringBuffer | append(Object obj) 追加 Object 參數的字符串表示形式。 |
StringBuffer | append(String str) 將指定的字符串追加到此字符序列。 |
StringBuffer | append(StringBuffer sb) 將指定的 StringBuffer 追加到此序列中。 |
StringBuffer | 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) 返回此序列指定文本范圍內的統一代碼點。 |
StringBuffer | delete(int start, int end) 移除此序列的子字符串中的字符。 |
StringBuffer | 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) 從指定的索引處開始,返回第一次出現的指定子字符串在該字符串中的索引。 |
StringBuffer | insert(int offset, boolean b) 將 boolean 參數的字符串表示形式插入此序列中。 |
StringBuffer | insert(int offset, char c) 將 char 參數的字符串表示形式插入此序列中。 |
StringBuffer | insert(int offset, char[] str) 將 char 數組參數的字符串表示形式插入此序列中。 |
StringBuffer | insert(int index, char[] str, int offset, int len) 將數組參數 str 的子數組的字符串表示形式插入此序列中。 |
StringBuffer | insert(int dstOffset, CharSequence s) 將指定 CharSequence 插入此序列中。 |
StringBuffer | insert(int dstOffset, CharSequence s, int start, int end) 將指定 CharSequence 的子序列插入此序列中。 |
StringBuffer | insert(int offset, double d) 將 double 參數的字符串表示形式插入此序列中。 |
StringBuffer | insert(int offset, float f) 將 float 參數的字符串表示形式插入此序列中。 |
StringBuffer | insert(int offset, int i) 將 int 參數的字符串表示形式插入此序列中。 |
StringBuffer | insert(int offset, long l) 將 long 參數的字符串表示形式插入此序列中。 |
StringBuffer | insert(int offset, Object obj) 將 Object 參數的字符串表示形式插入此字符序列中。 |
StringBuffer | 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 個代碼點后得到的。 |
StringBuffer | replace(int start, int end, String str) 使用給定 String 中的字符替換此序列的子字符串中的字符。 |
StringBuffer | 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() 嘗試減少用于字符序列的存儲空間。 |
3.1、StringBuilder簡述
用法與StringBuffer一樣
將 StringBuilder
的實例用于多個線程是不安全的。如果需要這樣的同步,則建議使用 StringBuffer
。
從 JDK 5 開始,為該類補充了一個單個線程使用的等價類,即 StringBuilder
。與該類相比,通常應該優先使用 StringBuilder 類,因為它支持所有相同的操作,但由于它不執行同步,所以速度更快。
3.2、String、StringBuffer和StringBuilder區別
String 長度是固定不變的
StringBuffer和StringBuilder是容器,長度是可變
StringBuffer是同步的是線程安全的。效率低 。多線程
StringBuilder是沒有同步的,非線程安全的,效率高. 單線程,或者手動加鎖 也可以用于多線程
升級:提高效率,提高安全,簡化寫代碼效率。建議使用StringBuilder
四、包裝類
4.1、基本數據類型對應的包裝類
byte Byte
short Short
int Interget
long Long
boolean Boolean
float Float
double Double
char Character
4.2、以int為類
字段摘要 | |
---|---|
static int | MAX_VALUE 值為 231-1 的常量,它表示 int 類型能夠表示的最大值。 |
static int | MIN_VALUE 值為 -231 的常量,它表示 int 類型能夠表示的最小值。 |
static int | SIZE 用來以二進制補碼形式表示 int 值的比特位數。 |
static Class<Integer> | TYPE 表示基本類型 int 的 Class 實例。 |
構造方法摘要 | |
---|---|
Integer(int value) 構造一個新分配的 Integer 對象,它表示指定的 int 值。 | |
Integer(String s) 構造一個新分配的 Integer 對象,它表示 String 參數所指示的 int 值。 |
package com.pb.demo.packclass.demo1;public class Interger { public static void main(String[] args) { Integer max=Integer.MAX_VALUE;//最大值 Integer min=Integer.MIN_VALUE;//最小值 System.out.println("max="+max); System.out.println("min="+min); }}
常用操作
用于基本數據類型和字符串類型之間做轉換
4.3、基本數據類型轉換成字符串
基本數據類型+"";
toString(基本數據類型);
String.ValueOf(基本數據類型)
package com.pb.demo.packclass.demo1;/** * 轉換為字符串 * @author Denny * */public class Demo1 { public static void main(String[] args) { double d=33.4; String s1=d+""; String s2=Double.toString(d); String s3=String.valueOf(d); }}
4.4、字符串轉換成基本數據類型
基本數據類型 值=基本類型類型的包裝類.parseXxx(String);
int a=Integer.parseInt("123");
package com.pb.demo.packclass.demo1;public class Demo2 { public static void main(String[] args) { String s1="32"; int i=Integer.parseInt(s1);//轉換為int類型 String s2="22.4"; double d=Double.parseDouble(s2);//轉換為double String s3="11.3"; float f=Float.parseFloat(s3);//轉換為float類型 long l=Long.parseLong("88888888"); boolean b=Boolean.parseBoolean("true");//或者false Integer x=new Integer(33); int num=x.intValue(); //動態方法 轉換為基本數據類型 }}
4.5、進制轉換
package com.pb.demo.packclass.demo1;public class Demo3 { public static void main(String[] args) { //轉換為二進制 System.out.println(Integer.toBinaryString(10)); //八進制 System.out.println(Integer.toOctalString(10)); //十六進制 System.out.println(Integer.toHexString(10)); //轉換為2進制 System.out.println(Integer.parseInt("110", 2)); //轉換為8進制 System.out.println(Integer.parseInt("17", 8)); //轉換為16進制 System.out.println(Integer.parseInt("af", 16)); }}
結果:
101012a615175
4.6、新特性
自動裝箱和自動拆箱
package com.pb.demo.packclass.demo1;public class Demo4 { public static void main(String[] args) { Integer x=4;// 自動裝箱,==new Integer(4), x=x+2;//自對拆箱 x.intValue()轉換為int類型,+2運算,再進行自動裝箱 } public static void sop(Object obj){ System.out.println(obj); }}
package com.pb.demo.packclass.demo1;public class Demo5 { public static void main(String[] args) { Integer i=128; Integer j=128; sop("i==j: "+(i==j));//false /* * 如果指向同一個Integr對象 * 當數值在byte范圍內里-128-127之間 * 1.5后的新特性 * 如果該值已經存在,就不會開辟新的空間 */ Integer x=127; Integer y=127; sop("x==y:"+(x==y));//true } public static void sop(Object obj){ System.out.println(obj); }}
int | compareTo(Integer anotherInteger) 在數字上比較兩個 Integer 對象。 |
package com.pb.demo.packclass.demo1;public class Demo6 { public static void main(String[] args) { Integer x=3; Integer y=8; sop(x.compareTo(y)); //-1 小于 sop(Integer.compare(y, x));//大于1 sop(Integer.compare(5, 5));//等于 0 } public static void sop(Object obj){ System.out.println(obj); }}
新聞熱點
疑難解答