String : 字符串類型java
1、構造函數
String(byte[ ] bytes):經過byte數組構造字符串對象。
String(char[ ] value):經過char數組構造字符串對象。
String(Sting original):構造一個original的副本。即:拷貝一個original。
String(StringBuffer buffer):經過StringBuffer數組構造字符串對象。
例如:
byte[] b = {'a','b','c','d','e','f','g','h','i','j'};
char[] c = {'0','1','2','3','4','5','6','7','8','9'};
String sb = new String(b); //abcdefghij
String sb_sub = new String(b,3,2); //de
String sc = new String(c); //0123456789
String sc_sub = new String(c,3,2); //34
String sb_copy = new String(sb); //abcdefghij
System.out.println("sb:"+sb);
System.out.println("sb_sub:"+sb_sub);
System.out.println("sc:"+sc);
System.out.println("sc_sub:"+sc_sub);
System.out.println("sb_copy:"+sb_copy);
輸出結果:sb:abcdefghij
sb_sub:de
sc:0123456789
sc_sub:34
sb_copy:abcdefghij程序員
2、方法:正則表達式
說明:①、全部方法均爲public。
②、書寫格式: [修飾符] <返回類型><方法名([參數列表])>數組
例如:static int parseInt(String s)
表示此方法(parseInt)爲類方法(static),返回類型爲(int),方法所須要爲String類型。app
1. char charAt(int index) :取字符串中的某一個字符,其中的參數index指的是字符串中序數。字符串的序數從0開始到length()-1 。
例如:String s = new String("abcdefghijklmnopqrstuvwxyz");
System.out.println("s.charAt(5): " + s.charAt(5) );
結果爲: s.charAt(5): f
2. int compareTo(String anotherString) :當前String對象與anotherString比較。相等關係返回0;不相等時,從兩個字符串第0個字符開始比較,返回第一個不相等的字符差,另外一種狀況,較長字符串的前面部分恰巧是較短的字符串,返回它們的長度差。
3. int compareTo(Object o) :若是o是String對象,和2的功能同樣;不然拋出ClassCastException異常。
例如:String s1 = new String("abcdefghijklmn");
String s2 = new String("abcdefghij");
String s3 = new String("abcdefghijalmn");
System.out.println("s1.compareTo(s2): " + s1.compareTo(s2) ); //返回長度差
System.out.println("s1.compareTo(s3): " + s1.compareTo(s3) ); //返回'k'-'a'的差
結果爲:s1.compareTo(s2): 4
s1.compareTo(s3): 10
4. String concat(String str) :將該String對象與str鏈接在一塊兒。
5. boolean contentEquals(StringBuffer sb) :將該String對象與StringBuffer對象sb進行比較。
6. static String copyValueOf(char[] data) :
7. static String copyValueOf(char[] data, int offset, int count) :這兩個方法將char數組轉換成String,與其中一個構造函數相似。
8. boolean endsWith(String suffix) :該String對象是否以suffix結尾。
例如:String s1 = new String("abcdefghij");
String s2 = new String("ghij");
System.out.println("s1.endsWith(s2): " + s1.endsWith(s2) );
結果爲:s1.endsWith(s2): true
9. boolean equals(Object anObject) :當anObject不爲空而且與當前String對象同樣,返回true;不然,返回false。
10. byte[] getBytes() :將該String對象轉換成byte數組。
11. void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) :該方法將字符串拷貝到字符數組中。其中,srcBegin爲拷貝的起始位置、srcEnd爲拷貝的結束位置、字符串數值dst爲目標字符數組、dstBegin爲目標字符數組的拷貝起始位置。
例如:char[] s1 = {'I',' ','l','o','v','e',' ','h','e','r','!'};//s1=I love her!
String s2 = new String("you!"); s2.getChars(0,3,s1,7); //s1=I love you!
System.out.println( s1 );
結果爲:I love you!
12. int hashCode() :返回當前字符的哈希表碼。
13. int indexOf(int ch) :只找第一個匹配字符位置。
14. int indexOf(int ch, int fromIndex) :從fromIndex開始找第一個匹配字符位置。
15. int indexOf(String str) :只找第一個匹配字符串位置。
16. int indexOf(String str, int fromIndex) :從fromIndex開始找第一個匹配字符串位置。
例如:String s = new String("write once, run anywhere!");
String ss = new String("run");
System.out.println("s.indexOf('r'): " + s.indexOf('r') );
System.out.println("s.indexOf('r',2): " + s.indexOf('r',2) );
System.out.println("s.indexOf(ss): " + s.indexOf(ss) );
結果爲:s.indexOf('r'): 1
s.indexOf('r',2): 12
s.indexOf(ss): 12
17. int lastIndexOf(int ch)
18. int lastIndexOf(int ch, int fromIndex)
19. int lastIndexOf(String str)
20. int lastIndexOf(String str, int fromIndex) 以上四個方法與13、14、15、16相似,不一樣的是:找最後一個匹配的內容。
public class CompareToDemo {
public static void main (String[] args) {
String s1 = new String("acbdebfg");
System.out.println(s1.lastIndexOf((int)'b',7));
}
}
運行結果:5
(其中fromIndex的參數爲 7,是從字符串acbdebfg的最後一個字符g開始往前數的位數。既是從字符c開始匹配,尋找最後一個匹配b的位置。因此結果爲 5)
21. int length() :返回當前字符串長度。
22. String replace(char oldChar, char newChar) :將字符號串中第一個oldChar替換成newChar。
23. boolean startsWith(String prefix) :該String對象是否以prefix開始。
24. boolean startsWith(String prefix, int toffset) :該String對象從toffset位置算起,是否以prefix開始。
例如:String s = new String("write once, run anywhere!");
String ss = new String("write");
String sss = new String("once");
System.out.println("s.startsWith(ss): " + s.startsWith(ss) );
System.out.println("s.startsWith(sss,6): " + s.startsWith(sss,6) );
結果爲:s.startsWith(ss): true
s.startsWith(sss,6): true
25. String substring(int beginIndex) :取從beginIndex位置開始到結束的子字符串。
26.String substring(int beginIndex, int endIndex) :取從beginIndex位置開始到endIndex位置的子字符串。
27. char[ ] toCharArray() :將該String對象轉換成char數組。
28. String toLowerCase() :將字符串轉換成小寫。
29. String toUpperCase() :將字符串轉換成大寫。
例如:String s = new String("java.lang.Class String");
System.out.println("s.toUpperCase(): " + s.toUpperCase() );
System.out.println("s.toLowerCase(): " + s.toLowerCase() );
結果爲:s.toUpperCase(): JAVA.LANG.CLASS STRING
s.toLowerCase(): java.lang.class string
30. static String valueOf(boolean b)
31. static String valueOf(char c)
32. static String valueOf(char[] data)
33. static String valueOf(char[] data, int offset, int count)
34. static String valueOf(double d)
35. static String valueOf(float f)
36. static String valueOf(int i)
37. static String valueOf(long l)
38. static String valueOf(Object obj)
以上方法用於將各類不一樣類型轉換成Java字符型。這些都是類方法。函數
Java中String類的經常使用方法:
public char charAt(int index)
返回字符串中第index個字符;
public int length()
返回字符串的長度;
public int indexOf(String str)
返回字符串中第一次出現str的位置;
public int indexOf(String str,int fromIndex)
返回字符串從fromIndex開始第一次出現str的位置;
public boolean equalsIgnoreCase(String another)
比較字符串與another是否同樣(忽略大小寫);
public String replace(char oldchar,char newChar)
在字符串中用newChar字符替換oldChar字符
public boolean startsWith(String prefix)
判斷字符串是否以prefix字符串開頭;
public boolean endsWith(String suffix)
判斷一個字符串是否以suffix字符串結尾;
public String toUpperCase()
返回一個字符串爲該字符串的大寫形式;
public String toLowerCase()
返回一個字符串爲該字符串的小寫形式
public String substring(int beginIndex)
返回該字符串從beginIndex開始到結尾的子字符串;
public String substring(int beginIndex,int endIndex)
返回該字符串從beginIndex開始到endsIndex結尾的子字符串
public String trim()
返回該字符串去掉開頭和結尾空格後的字符串
public String[] split(String regex)
將一個字符串按照指定的分隔符分隔,返回分隔後的字符串數組
實例:
public class SplitDemo{
public static void main (String[] args) {
String date = "2008/09/10";
String[ ] dateAfterSplit= new String[3];
dateAfterSplit=date.split("/"); //以「/」做爲分隔符來分割date字符串,並把結果放入3個字符串中。
for(int i=0;i<dateAfterSplit.length;i++)
System.out.print(dateAfterSplit[i]+" ");
}
}
運行結果:2008 09 10 //結果爲分割後的3個字符串spa
實例:
TestString1.java:
程序代碼
public class TestString1
{
public static void main(String args[]) {
String s1 = "Hello World" ;
String s2 = "hello world" ;
System.out.println(s1.charAt(1)) ;
System.out.println(s2.length()) ;
System.out.println(s1.indexOf("World")) ;
System.out.println(s2.indexOf("World")) ;
System.out.println(s1.equals(s2)) ;
System.out.println(s1.equalsIgnoreCase(s2)) ;對象
String s = "我是J2EE程序員" ;
String sr = s.replace('我','你') ;
System.out.println(sr) ;
}
}繼承
TestString2.java:
程序代碼索引
public class TestString2
{
public static void main(String args[]) {
String s = "Welcome to Java World!" ;
String s2 = " magci " ;
System.out.println(s.startsWith("Welcome")) ;
System.out.println(s.endsWith("World")) ;
String sL = s.toLowerCase() ;
String sU = s.toUpperCase() ;
System.out.println(sL) ;
System.out.println(sU) ;
String subS = s.substring(11) ;
System.out.println(subS) ;
String s1NoSp = s2.trim() ;
System.out.println(s1NoSp) ;
}
String類主要方法的使用:
1、獲取長度 *.length();//這與數組中的獲取長度不一樣,*.length;
2、比較字符串(1) equals() //判斷內容是否相同
(2)compareTo() //判斷字符串的大小關係
(3)compareToIgnoreCase(String int) //在比較時忽略字母大小寫
(4)== //判斷內容與地址是否相同
(5)equalsIgnoreCase() //忽略大小寫的狀況下判斷內容是否相同
若是想對字符串中的部份內容是否相同進行比較,能夠用
(6)reagionMatches() //有兩種 public boolean regionMatches(int toffset, String other,int ooffset,int len);表示若是String對象的一個子字符串與參數other的一個子字符串是相同的字符序列,則爲true.要比較的String 對象的字符串從索引toffset開始,other的字符串從索引ooffset開始,長度爲len。
public boolean reagionMatches(boolean ignoreCase,int toffset,String other,int ooffset,int len);//用布爾類型的參數指明兩個字符串的比較是否對大小寫敏感。
1、查找字符串中某個位置的字符
public char charAt(int index);//返回指定索引index位置上的字符,索引範圍從0開始
4、查找指定字符串在字符串中第一次或最後一詞出現的位置
在String類中提供了兩種查找指定位置的字符串第一次出現的位置的方法
(1)public int indexOf(String str);//從字符串開始檢索str,並返回第一次出現的位置,未出現返回-1
(2)public int indexOf(String str,int fromIndex);//從字符串的第fromIndex個字符開始檢索str
查找最後一次出現的位置有兩種方法
(1)public int lastIndexOf(String str);
(2)public int lastIndexOf(String str,int fromIndex);
若是不關心字符串的確切位置則可以使用public boolean contains(CharSequence s);
2、檢查字符串的起始字符和結束字符
開始的字符串兩種方法
(1)public boolean starWith(String prefix,int toffset);//若是參數prefix表示的字符串序列是該對象從索引toffset處開始的子字符串,則返回true
(2)public boolean starWith(String prefix);
結束的字符串方法
public boolean endsWith(String suffix);
3、截取子串
(1)public String subString(int beginIndex);
(2)public String subString(int beginIndex,int endIndex);//返回的字符串是從beginIndex開始到endIndex-1的串
要返回後4位能夠這樣寫Syetem.out.println(*.subString()(*.length()-4));
4、字符串的替換
兩種方法
(1)public String replace(char oldChar,char newChar);
(2)public String replace(CharSequence target,CharSequence replacement);//把原來的etarget子序列替換爲replacement序列,返回新串
(3)public String replaceAll(String regex,String replacement);//用正則表達式實現對字符串的匹配
5、字符串的大小寫替轉換
(1)public String toLowerCase(Locale locale);
(2)public String toLowerCase();
(3)public String toupperCase(Locale locale);
(4)public String toUpperCase();
6、去除字符串首尾空格
*.trim();
7、字符串轉換
1、將字符串轉換成字符數組
public char[] toCharArray();
2、將字符串轉換成字符串數組
public String[] split(String regex);//regex 是給定的匹配
3、將其它數據類型轉化爲字符串
(1)public static String valueOf(boolean b);
(2)public static String valueOf(char c);
(3)public static String valueOf(int i);
(4)public static String valueOf(long i);
(5)public static String valueOf(float f);
(6)public static String valueOf(double d);
(7)public static String valueOf(char[] data);
(8)public static String valueOf(Object obj);
可變字符串的建立和初始化
兩種方法:
public StringBuffer();
public StringBuffer(int caoacity);
StringBuffer類主要方法的使用:
1、獲取可變字符串長度
(1)public int length();
(2)public int capacity();
(3)public void setLength(int newLength);
2、比較可變字符串
用String 類的equals()方法比較,可是不一樣。
類Object中的equals()方法比較的是兩個對象的地址是否相等,而不單單是比較內容,可是String類在繼承Object類的時候重寫了equals()方法,只是比較兩個對象的內容是否相等
而在StringBuffer類中沒有重寫Object類的equals()方法,因此比較的是地址和內容。
3、追加和插入字符串
(1)追加 public StringBuffer append(type t);
(2)插入 public StringBuffer insert(int offset,type t);//在offset處加入類型爲type的字符串
4、反轉和刪除字符串
(1)反轉 public StringBuffer reverse();
(2)刪除 public StringBuffer delete(int start,int end);
5、減小用於可變字符序列的存儲空間
public void trimToSize();
6、StringBuffer類轉換成String類
public String toString();