Character,String相關方法,Int,double互相轉換


/*
 * 
 * String相關方法
 * 
 */
public class StringTest2 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String a ="abc123";
        String b ="Abc123";
        
        if(a.equalsIgnoreCase(b)){
            System.out.println("兩個對象內容相等");
        }else{
            System.out.println("兩個對象內容不相等");
        }
        
        int iii = b.compareTo(a); //逐個比較字符串,如果發現一模一樣返回零,當發現一個不一樣的時候,就返回兩個值
                                   //ascii碼的差,並停止比較
        System.out.println(iii);
        
         String url ="http://www.163.com";
         //判斷是否以指定字符串開頭,指定字符結尾
         if(url.startsWith("http") && url.endsWith("com")){
             System.out.println("ok");
         }else{
             System.out.println("bu ok");
         }
         
         //分割字符串
         //substring ,split
         String sql = "select * from abc";
         //得到select
         System.out.println(sql.substring(0,6));
         System.out.println(sql.substring(7));
         //使用空格來區分
         String[] aaa = sql.split(" ");
         
         for(String str : aaa){
             System.out.println(str);
         }
         
         System.out.println(sql.charAt(1));//得到指定索引位的字符
         System.out.println(sql.indexOf('e'));
         
         String s = sql.replaceAll("e", "a");
         System.out.println(s);
    }

}/*
*
*   包裝類...承基本類型啟String
*   Char包裝類有很多字符的操作 
*   String的不變性,字符串常量區的存在
*   String相加使用StringBuffer
*       StringBuffer sb = new StringBuffer(25000);//這個是默認創建一個可以存放16個字符的sb,這里創建了25000
*      sb.append("字符串").append("字符串");
*      字符串加完之后 String s = sb.toString();
*      
*   String相關的方法:對字符串的操作   
*
*/
public class CharacterTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        char c = 'a';
        
        if(Character.isDigit(c)){
            System.out.println("是一個數字");
        }
        if(Character.isLetter(c)){
            System.out.println("是一個字母");
        }
    }
}
public class DoubleTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        //double 和 Double 
        
//        int i = 5;
//        Integer ii = new Integer(5);
//        
        double d = 12.34;
        
        Double dou = new Double(d);
        Double dou1 = Double.valueOf(d);    
        //int iii = ii.intValue(); 
        double d1 = dou.doubleValue();
        
        
        //double和String之間
//        String s  = Integer.toString(i);
//        System.out.println(s);
        String s = Double.toString(d);
        
       // int iiii = Integer.parseInt(s);
         double d2 = Double.parseDouble(s);
//         
//       //String 和Integer的關系處理
//         //1.String轉Integer
//         Integer iiiii = Integer.valueOf(s);
//         System.out.println(iiiii);
//         
//         //2.Integer轉String
//         String ss = iiiii.toString();  
         Double d4 = Double.valueOf(s);
         String ss = d4.toString();
    }

}
public class IntegerTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        //int 和 integer(int 的包裝類)之間的轉化
        
        //1. int轉integer
        int i = 5;
        Integer ii = new Integer(5);
        
        //2. integer轉int
        
        int iii = ii.intValue(); 
        System.out.println(iii);
        
        //int和String之間的轉換
        //1. int轉String
        String s  = Integer.toString(i);
        System.out.println(s);
        //2.String轉int
        int iiii = Integer.parseInt(s);
        
        
        //String 和Integer的關系處理
        //1.String轉Integer
        Integer iiiii = Integer.valueOf(s);
        System.out.println(iiiii);
        
        //2.Integer轉String
        String ss = iiiii.toString();
    }

}
import java.lang.reflect.Array;
import java.util.Arrays;


public class StringTest {
    
    public static void main(String[] args) {
        
        String s = "abccd";
        String s0 = new String();//不要使用NEW來創建字符串
        
        
        System.out.println(s.length());
        
        IntegerTest it = new IntegerTest();
        IntegerTest it1 = new IntegerTest();
    
//        String s1 = "abccd";
//        if(it == it1){
//            System.out.println("相等");
//        }
//        
//        if(s == s1){
//            
//            System.out.println("相等");
//            
//            
//        }
        int a = 4;
        int b = 4;
        System.out.println(a==b);
        
        int a1[] = new int[]{1,2,3};
        int b1[] = new int[]{1,2,3};
        System.out.println(a1==b1);
        System.out.println(Arrays.equals(a1, b1));//數組equals比較奇怪 
        
        String a2 = new String("abcdefg");//是new 出來的不會放到字符串常量區
        String b2 = new String("abcdefg");

        System.out.println(a2==b2);
        System.out.println(a2.equals(b2));//標准 equals調用
        
        String a3 = "abcdefgh";
        String b3 = "abcdefgh";
        System.out.println(a3==b3);
        System.out.println(a3.equals(b3));//標准 equals調用
                
        //String的"bug"特性
        //new 就會在內存的堆開地盤
        //當我們以String a3 = "abcdefgh"創建字符串的時候
        //以String a3 = 這種方式創建 時,JVM會檢查字符串常量區是否有"abcdefgh",這個字符串,
        //如果沒有就創建一個放在里面,如果有的話,就將a3指向它
        
        //應為這個特性帶來的麻煩:那就是相加之后的字符串,並不是在原來字符串基礎上變化,而是保留原來的字符串
        //創建一個新的字符串,然后指向變量,
        //這樣子的話如果大量的相加操作出現就在常量區中創建非常多的無用字符串,內存上是種浪費
        
        //String這樣的特性原本就是為了節省內存,比如一個字符串有100萬字符,現在又有一個相同100萬字符,
        //有了這個特性,我只要存一個字符串就可以
        //所以有一個解決字符串相加的方案,就是使用一個叫做StringBuffer對象
        String a4 = "abcdefg"+"123456";
        a4 = a4+"aaa";
        
    /*   for(int i =0 ;i<10000;i++){
           
           a4 = a4+"a";
       }這段代碼會產生10001個字符串,10000個無用的*/
        
      StringBuffer sb = new StringBuffer(a4);
      for(int i = 0 ;i <10000 ;i++){
          sb.append("a").append("1").append("222b").append("1233333fff");
      }//只會產生一個sb對象
      a4 = sb.toString();//將結果給a4
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM