標籤: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-測試開發字串