java-測試開發字串

來源:互聯網
上載者:User

標籤:ring   void   first   blog   length   運行   div   new   beautiful   

package j2se;public class StringDemo {private String demoString="ceshixiaoyouning";public void testString(){String dsg=demoString.concat("very beautiful");//字串相加,也可以用+來表示System.out.println(dsg);int len=demoString.length();//字串的長度System.out.println(len);boolean eq="ceshixiaoyouning".equals(demoString);//比較兩個字串是否相等System.out.println(eq);String sub=demoString.substring(5, 8);//取子字串,從第5個字元開始,到底8個字元,但是不包含第8個字元System.out.println(sub);String subString=demoString.substring(5);//取子字串,從第5個字元開始一直到字串尾System.out.println(subString);boolean sw =demoString.startsWith("ceshi");//判斷是否以某個字串開頭System.out.println(sw);boolean ew=demoString.endsWith("youning");//判斷石頭以某個字串結尾System.out.println(ew);int subIndex=demoString.indexOf("ce");//找出子字串在字串中第一次出現的index,如果找不到則返回-1System.out.println(subIndex);int lastIndex=demoString.lastIndexOf("i");//找出子字串在字串最後一次出現的index,如果找不到則返回-1System.out.println(lastIndex);System.out.println(demoString.toUpperCase());//字串中的字全變成大寫System.out.println(demoString.toLowerCase());//字串中的字全變成小寫System.out.println("    youning   ".trim());//將字串首尾的空格去掉String subReplace=demoString.replace("ceshi", "hello");//將字串中的某段字串替換成新的字串System.out.println(subReplace);String subReplaceF=demoString.replaceFirst("i", "hhh");//將字串中第一次出現的子字串替換成新的字串,支援正則System.out.println(subReplaceF);String subReplaceA=demoString.replaceAll("i", "hhh");//將字串中出現的所有子字串替換成新的子字串,支援正則System.out.println(subReplaceA);}public static void main(String[] args) {StringDemo s=new StringDemo();s.testString();}}

 運行結果:

ceshixiaoyouningvery beautiful
16
true
xia
xiaoyouning
true
true
0
13
CESHIXIAOYOUNING
ceshixiaoyouning
youning
helloxiaoyouning
ceshhhhxiaoyouning
ceshhhhxhhhaoyounhhhng

java-測試開發字串

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.