java例程练习(String类的一些常用方法)

//String类常用方法集锦

public class TestString { public static void main(String[] args) { String s1 = "sun java"; String s2 = "Sun Java"; //取字符 System.out.println(s1.charAt(1));//u //长度 System.out.println(s2.length());//8 //子串位置 System.out.println(s1.indexOf("java"));//4 System.out.println(s1.indexOf("Java"));//-1 //与什么相等 System.out.println(s1.equals(s2));//false System.out.println(s1.equalsIgnoreCase(s2));//true //字符替换 String s = "我是程序员,我在学java"; String sr = s.replace('我', '你'); System.out.println(sr);//你是程序员,你在学java //以什么字符串开头或结尾 String ss = "Welcome to Java World!"; System.out.println(ss.startsWith("Welcome"));//true System.out.println(ss.endsWith("World"));//false //转成大写或小写 String ssL = ss.toLowerCase(); String ssU = ss.toUpperCase(); System.out.println(ssL);//welcome to java world! System.out.println(ssU);//WELCOME TO JAVA WORLD! //截子串 String subSS = ss.substring(11); System.out.println(subSS);//Java World! //去掉首位空格 String st = " sun java "; String sp = st.trim(); System.out.println(sp);//sun java //valueOf静态方法求数字位数 int j = 1234567; String sNumber = String.valueOf(j); System.out.println("j是" + sNumber.length() + "位数");//j是7位数 //分割字符 String bd = "Mary,F,1976"; String [] bdSplit = bd.split(","); for(int i = 0; i < bdSplit.length; i++) { System.out.println(bdSplit[i]); } // Mary // F // 1976 } }

 
   
 
  

转载于:https://www.cnblogs.com/springside5/archive/2012/04/30/2486233.html

你可能感兴趣的:(java例程练习(String类的一些常用方法))