String相关知识

package com.lang;
import java.lang.reflect.Array;
import java.util.Arrays;


public class StringTest {
    
    public void abc(){
        System.out.println("abc");
    }
    
    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
    }
    
    
    
   
}
package com.lang;
/*
 * 
 * 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相关的方法:对字符串的操作   
*
*/

 

posted @ 2012-10-19 11:25  邹晟  阅读(135)  评论(0编辑  收藏  举报