1. 程式人生 > >Java-String類的常用方法總結

Java-String類的常用方法總結

一、String類
String類在java.lang包中,java使用String類建立一個字串變數,字串變數屬於物件。java把String類宣告的final類,不能有類。String類物件建立後不能修改,由0或多個字元組成,包含在一對雙引號之間。
二、String類物件的建立
字串宣告:String stringName;
字串建立:stringName = new String(字串常量);或stringName = 字串常量;
三、String類構造方法
1、public String()
無參構造方法,用來建立空字串的String物件。
 1 String str1 = new String(); 
2、public String(String value)


用已知的字串value建立一個String物件。
 1 String str2 = new String("asdf"); 2 String str3 = new String(str2); 
3、public String(char[] value)
用字元陣列value建立一個String物件。

1 char[] value = {"a","b","c","d"};
2 String str4 = new String(value);//相當於String str4 = new String("abcd");


4、public String(char chars[], int startIndex, int numChars)
用字元陣列chars的startIndex開始的numChars個字元建立一個String物件。

1 char[] value = {"a","b","c","d"};
2 String str5 = new String(value, 1, 2);//相當於String str5 = new String("bc");


5、public String(byte[] values)
用位元陣列values建立一個String物件。

1 byte[] strb = new byte[]{65,66};
2 String str6 = new String(strb);//相當於String str6 = new String("AB");

四、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>0
4 int b = str1.compareTo(str2);//b=0
5 boolean c = str1.equals(str2);//c=false
6 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 = 2
3 int b = str.indexOf("good");//b = 7
4 int c = str.indexOf("w",2);//c = -1
5 int d = str.lastIndexOf("a");//d = 5
6 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 = 6
4 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 = true
3 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");//true
3 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、基本型別轉換為字串型別
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、進位制轉換
使用Long類中的方法得到整數之間的各種進位制轉換的方法:
Long.toBinaryString(long l)
Long.toOctalString(long l)
Long.toHexString(long l)
Long.toString(long l, int p)//p作為任意進位制