java string類

來源:互聯網
上載者:User

標籤:tin   concat   比較   中間   bst   取出   重載   修改   字串串連   

 

一、String類
String類在java.lang包中,java使用String類建立一個字串變數,字串變數屬於對象。java把String類聲明的final類,不能有類。String類對象建立後不能修改,由0或多個字元組成,包含在一對雙引號之間。
二、String類對象的建立
字串聲明:String stringName;
字串建立:stringName = new String(字串常量);或stringName = 字串常量;
三、String類構造方法
1、public String()
無參構造方法,用來建立Null 字元串的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>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";

 l, int p)//p作為任意進位

java string類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.