java基礎——String的常用方法

來源:互聯網
上載者:User

標籤:false   int start   java   array   tin   一個   ast   ==   har   

java中String的常用方法1、length() 字串的長度   例:char chars[]={‘a‘,‘b‘.‘c‘};     String s=new String(chars);     int len=s.length();2、charAt() 截取一個字元   例:char ch;     ch="abc".charAt(1); 返回‘b‘
3、 getChars() 截取多個字元    例:String s="this is a demo of the getChars method.";    char buf[]=new char[20];      s.getChars(10,14,buf,0);
4、getBytes()   替代getChars()的一種方法是將字元儲存在位元組數組中,該方法即getBytes()。
5、toCharArray()
6、equals()

判斷兩個String的值 是否相等

equalsIgnoreCase()

判斷兩個String的值 是否相等 並且忽略大小寫


7、regionMatches()  用於比較一個字串中特定地區與另一特定地區,它有一個重載的形式允許在比較中忽略大小寫。   
8、startsWith()和endsWith()   startsWith()方法決定是否以特定字串開始,endWith()方法決定是否以特定字串結束
9、equals()和==    equals()方法比較字串對象中的字元,==運算子比較兩個對象是否引用同一執行個體。   例:String s1="Hello";     String s2=new String(s1);   s1.eauals(s2); //true     s1==s2;//false
10、compareTo()和compareToIgnoreCase() 比較字串
11、indexOf()和lastIndexOf()   indexOf() 尋找字元或者子串第一次出現的地方。   lastIndexOf() 尋找字元或者子串是後一次出現的地方。12、substring()  它有兩種形式,第一種是:String substring(int startIndex)          第二種是:String substring(int startIndex,int endIndex)13、concat() 串連兩個字串14 、replace() 替換   它有兩種形式,第一種形式用一個字元在調用字串中所有出現某個字元的地方進行替換,形式如下:   String replace(char original,char replacement)   例如:String s="Hello".replace(‘l‘,‘w‘);   第二種形式是用一個字元序列替換另一個字元序列,形式如下:   String replace(CharSequence original,CharSequence replacement)15、trim() 去掉起始和結尾的空格16、valueOf() 轉換為字串17、toLowerCase() 轉換為小寫18、toUpperCase() 轉換為大寫
 

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.