標籤:hex 重載方法 boolean .com 返回 執行個體 from value Fix
一.常用的建立方式
String s1=”abc“;
String s2=”abc“;
s1==s2 ==> true
解析:s1和s2指向的是同一個字串池地址
二.不常用的建立方式
String s1=new String(”abc“);
String s2=new String(”abc“);;
s1==s2 ==> false
解析:s1和s2指向的是不同的字串池地址,new的時候會重新建立一個新的記憶體空間裝載字串,哪怕之前有一樣的
三.String的特點
(1).String對象是不可變的(常量);
(2)類中的每一個看來回修改S聽值得方法,其實都是建立了新的String對象(包含修改後的字串內容);
(3)String的唯讀特性帶來的效率最佳化可能;
(4)字串字面值儲存與字串池中,String對象優先指向該字元池,避免反覆產生重複的字串執行個體;
(5)系統對String的非修改處理效率很高,遠遠超過另外兩個字串類StringBuffer和StringBuilder(後續講解)
四.String對象的常用方法
1、求字串長度
public int length()//返回該字串的長度
1 String str = new String("asdfzxc");2 int strlength = str.length();//strlength = 7
2、求字串某一位置字元
public char charAt(int index)//返回字串中指定位置的字元;注意字串中第一個字元索引是0,最後一個是length()-1。
1 String str = new String("asdfzxc");2 char ch = str.charAt(4);//ch = z
3、提取子串
用String類的substring方法可以提取字串中的子串,該方法有兩種常用參數:
1)public String substring(int beginIndex)//該方法從beginIndex位置起,從當前字串中取出剩餘的字元作為一個新的字串返回。
2)public String substring(int beginIndex, int endIndex)//該方法從beginIndex位置起,從當前字串中取出到endIndex-1位置的字元作為一個新的字串返回。
1 String str1 = new String("asdfzxc");2 String str2 = str1.substring(2);//str2 = "dfzxc"3 String str3 = str1.substring(2,5);//str3 = "dfz"
4、字串比較
1)public int compareTo(String anotherString)//該方法是對字串內容按字典順序進行大小比較,通過返回的整數值指明當前字串與參數字串的大小關係。若當前對象比參數大則返回正整數,反之返回負整數,相等返回0。
2)public int compareToIgnore(String anotherString)//與compareTo方法相似,但忽略大小寫。
3)public boolean equals(Object anotherObject)//比較當前字串和參數字串,在兩個字串相等的時候返回true,否則返回false。
4)public boolean equalsIgnoreCase(String anotherString)//與equals方法相似,但忽略大小寫。
1 String str1 = new String("abc");2 String str2 = new String("ABC");3 int a = str1.compareTo(str2);//a>04 int b = str1.compareTo(str2);//b=05 boolean c = str1.equals(str2);//c=false6 boolean d = str1.equalsIgnoreCase(str2);//d=true
5、字串串連
public String concat(String str)//將參數中的字串str串連到當前字串的後面,效果等價於"+"。
1 String str = "aa".concat("bb").concat("cc");2 相當於String str = "aa"+"bb"+"cc";
6、字串中單個字元尋找
1)public int indexOf(int ch/String str)//用於尋找當前字串中字元或子串,返回字元或子串在當前字串中從左邊起首次出現的位置,若沒有出現則返回-1。
2)public int indexOf(int ch/String str, int fromIndex)//改方法與第一種類似,區別在於該方法從fromIndex位置向後尋找。
3)public int lastIndexOf(int ch/String str)//該方法與第一種類似,區別在於該方法從字串的末尾位置向前尋找。
4)public int lastIndexOf(int ch/String str, int fromIndex)//該方法與第二種方法類似,區別於該方法從fromIndex位置向前尋找。
1 String str = "I am a good student";2 int a = str.indexOf(‘a‘);//a = 23 int b = str.indexOf("good");//b = 74 int c = str.indexOf("w",2);//c = -15 int d = str.lastIndexOf("a");//d = 56 int e = str.lastIndexOf("a",3);//e = 2
7、字串中字元的大小寫轉換
1)public String toLowerCase()//返回將當前字串中所有字元轉換成小寫後的新串
2)public String toUpperCase()//返回將當前字串中所有字元轉換成大寫後的新串
1 String str = new String("asDF");2 String str1 = str.toLowerCase();//str1 = "asdf"3 String str2 = str.toUpperCase();//str2 = "ASDF"
8、字串中字元的替換
1)public String replace(char oldChar, char newChar)//用字元newChar替換當前字串中所有的oldChar字元,並返回一個新的字串。
2)public String replaceFirst(String regex, String replacement)//該方法用字元replacement的內容替換當前字串中遇到的第一個和字串regex相匹配的子串,應將新的字串返回。
3)public String replaceAll(String regex, String replacement)//該方法用字元replacement的內容替換當前字串中遇到的所有和字串regex相匹配的子串,應將新的字串返回。
1 String str = "asdzxcasd";2 String str1 = str.replace(‘a‘,‘g‘);//str1 = "gsdzxcgsd"3 String str2 = str.replace("asd","fgh");//str2 = "fghzxcfgh"4 String str3 = str.replaceFirst("asd","fgh");//str3 = "fghzxcasd"5 String str4 = str.replaceAll("asd","fgh");//str4 = "fghzxcfgh"
9、其他類方法
1)String trim()//截去字串兩端的空格,但對於中間的空格不處理。
1 String str = " a sd ";2 String str1 = str.trim();3 int a = str.length();//a = 64 int b = str1.length();//b = 4
2)boolean statWith(String prefix)或boolean endWith(String suffix)//用來比較當前字串的起始字元或子字串prefix和終止字元或子字串suffix是否和當前字串相同,重載方法中同時還可以指定比較的開始位置offset。
1 String str = "asdfgh";2 boolean a = str.statWith("as");//a = true3 boolean b = str.endWith("gh");//b = true
3)regionMatches(boolean b, int firstStart, String other, int otherStart, int length)//從當前字串的firstStart位置開始比較,取長度為length的一個子字串,other字串從otherStart位置開始,指定另外一個長度為length的字串,兩字串比較,當b為true時字串不區分大小寫。
4)contains(String str)//判斷參數s是否被包含在字串中,並返回一個布爾類型的值。
1 String str = "student";2 str.contains("stu");//true3 str.contains("ok");//false
5)String[] split(String str)//將str作為分隔字元進行字串分解,分解後的字字串在字串數組中返回。
1 String str = "asd!qwe|zxc#";2 String[] str1 = str.split("!|#");//str1[0] = "asd";str1[1] = "qwe";str1[2] = "zxc";
五、字串與基本類型的轉換
1、字串轉換為基本類型
java.lang包中有Byte、Short、Integer、Float、Double類的調用方法:
1)public static byte parseByte(String s)
2)public static short parseShort(String s)
3)public static short parseInt(String s)
4)public static long parseLong(String s)
5)public static float parseFloat(String s)
6)public static double parseDouble(String s)
例如:
1 int n = Integer.parseInt("12");2 float f = Float.parseFloat("12.34");3 double d = Double.parseDouble("1.124");
2、進位轉換
使用Long類中的方法得到整數之間的各種進位轉換的方法:
Long.toBinaryString(long l)
Long.toOctalString(long l)
Long.toHexString(long l)
Long.toString(long l, int p)//p作為任意進位
3、基本類型轉換為字串類型
String類中提供了String valueOf()放法,用作基本類型轉換為字串類型。
1)static String valueOf(char data[])
2)static String valueOf(char data[], int offset, int count)
3)static String valueOf(boolean b)
4)static String valueOf(char c)
5)static String valueOf(int i)
6)static String valueOf(long l)
7)static String valueOf(float f)
8)static String valueOf(double d)
例如:
1 String s1 = String.valueOf(12);2 String s1 = String.valueOf(12.34);
3.Java基礎:String對象的建立和使用