java-測試開發字符串
阿新 • • 發佈:2017-05-26
bst 新的 beautiful stat 測試 dem efi main highlight
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,如果找不到則返回-1 System.out.println(subIndex); int lastIndex=demoString.lastIndexOf("i");//找出子字符串在字符串最後一次出現的index,如果找不到則返回-1 System.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-測試開發字符串