JAva Stringメソッド解析

9949 ワード

import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Date;
import java.util.Locale;
import java.util.regex.PatternSyntaxException;

public class StringTest {

	public static void main(String[] args) {
		
		//charAt(int index)   index  Unicode  
		   
		String strCom ="JAVA    ";
		 System.out.println(strCom.charAt(4));
		 //  --------  
		 
		 
		//codePointAt(int index)   index    Unicode   
		 strCom = "I like JAVA";
		 
		 System.out.println(strCom.codePointAt(8));
		 
		  //codePointBefore   index-1    Unicode   
		    System.out.println(strCom.codePointBefore(2));
		    
		    //codePointCount(int beginIndex,int endIndex)               Unicode      
		    System.out.println(strCom.codePointCount(0, 3));
		    
		  //compareTo(String str)
		    //         ,                 ,      ,           
		    //               ,  k         ,                k 
		    //  char   ,             ,              
		    System.out.println(strCom.compareTo("I like PHP"));
		    
		    System.out.println(strCom.compareTo("I like JAVA too"));
		    
		  //compareToIgnoreCase(String str)                
		    System.out.println(strCom.compareToIgnoreCase("I Like PHP"));
		    
		    //concat(String str)                 ,           0,
		    //       ,        String  
		    System.out.println(strCom.equals(strCom.concat("")));
		    System.out.println(strCom.concat(strCom));
		    
		    //contains(CharSequence s)            char   
		    System.out.println(strCom.contains("JAVA"));
		    
		    
		    
		    
		 
		    //contentEquals  
		    String str1 = "amrood admin";
		   String str2 = "amrood admin";
		    
		    /* string represents the same sequence of characters as the 
		    specified StringBuffer */
		    StringBuffer strbuf = new StringBuffer(str1);
		    System.out.println("Method returns : " + str2.contentEquals(strbuf));
		    System.out.println(str2.equals(strbuf));
		  
		    /* string does not represent the same sequence of characters as the 
		    specified StringBuffer */
		    str2 = str1.toUpperCase();
		    System.out.println("Method returns : " + str2.contentEquals(strbuf));
		    
		    //copyValueOf valueOff    
		    char[] c={'a','b','c','e'};
		    System.out.println(c);
		    System.out.println(c.toString());
		    System.out.println(String.copyValueOf(c));//   c           ,   String s=new String(c); ,        "abcd"   
		    System.out.println(String.copyValueOf(c,2,2));//   c    2   (  'c')  ,   2         ,        "cd"   
		    
		    
		  //valueOf(char []data)     ,               
		    char [] array={' ',' '};
		    System.out.println(String.valueOf(array));
		    
		  //valueOf(char[] data,int offset,int count)         offset    count   
		    //      
		    System.out.println(String.valueOf(array, 0, 1));
		    
		    //endwith(String suffix)               
		    System.out.println(strCom.endsWith("too"));
		    
		    //equals(object obj)              String  String  ,   true,  false
		    System.out.println(strCom.equals("I like JAVA"));
		    
		    //equalsIgnoreCase(String anotherString) //               ,   equals         
		    System.out.println(strCom.equalsIgnoreCase("I Like JAva"));
		    
		    //format(String format,Object ...args)                               
		    //%d          
		    //%o          
		    //%x %X           
		    
		    System.out.println(String.format("%e %x %o %d %a %% %n", 15.000,15,15,15,15.0));
		    
		    
		    
		  //format(Locale l,String format,Object ... args)
		    //                              
		    //%te         
		    //%tb            
		    //%tB            
		    //%tA             
		    //%ta             
		    //%tc            
		    //%tY 4   
		    //%ty     
		    //%tm   
		    //%tj        
		    //%td         
		    Date date = new Date(); 
		    Locale form = Locale.CHINA;
		    String year = String.format(form, "%tY", date);
		    String month    = String.format(form, "%tm", date);
		    String day = String.format(form, "%td", date);
		    System.out.println("   : "+ year + " "+month+" "+day+" ");
		    System.out.println(String.format(form, "%tc", date));
		    
		    //byte[] getBytes()       byte      
		    byte[] str = strCom.getBytes();
		    for (int i = 0;i < str.length;i++){
		      System.out.print(str[i]+" ");
		    }
		    
		    
		    //getBytes(Charset charset)
		    //getBytes(String string)
		    //              
		    try {
		      str = strCom.getBytes(Charset.defaultCharset());
		      for (int i = 0; i < str.length; i++)
		        System.out.println(str[i] + " ");
		    } catch (UnsupportedCharsetException e) {
		      // TODO: handle exception
		      e.printStackTrace();
		    }
		    
		  //getchars(int srcBegin,int srcEnd,char[] dst,int dstBegin)
		    //                 
		    char[] dst = new char[10];
		    strCom.getChars(2, 9, dst, 3);
		    for (int i = 0; i < dst.length;i++)
		      System.out.print(dst[i]);
		    System.out.println();
		    
		  //hashCode()             ,String            
		    //s[0]*31^(n-1)+s[1]*31^(n-2)+...+s[n-1] 
		    //       0
		 	System.out.println(strCom.hashCode());
		 	
		 	 //indexOf(int ch)           ,ch     ,    ,  -1
		    System.out.println(strCom.indexOf('A'));
		    
		  //indexOf(int ch,int fromIndex)    //                    
		    //fromIndex    ,    , 0  ,           ,   -1
		    System.out.println(strCom.indexOf('A', 9));
		    
		    //indexOf(String str)
		    //indexOf(String str,int fromIndex)
		    //                     
		    System.out.println(strCom.indexOf("JAVA"));
		    
		    //intern()                   
		    //   intern   ,            String      ,         
		    //              ,    String    
		    //                                          。
		    System.out.println("intern()intern()intern()intern()intern()");
		    String strCom2 = new String("I like JAVA");
		    System.out.println(strCom == strCom2);
		    System.out.println(strCom.endsWith(strCom2));
		    System.out.println(strCom.compareTo(strCom2));
		    System.out.println(strCom.intern() == strCom2.intern());
		    String s1 = new String("  ,Java   ");
		    String s2 = new String("  ,") + "Java   ";
		    System.out.println(s1==s2);
		    System.out.println(s1.intern()==s2.intern());
		    
		    // indexOf,  fromIndex   ,   fromIndex     
		    System.out.println(strCom.lastIndexOf('A'));
		    System.out.println(strCom.lastIndexOf('A',10));
		    System.out.println(strCom.lastIndexOf("JAVA"));
		    System.out.println(strCom.lastIndexOf("JAVA", 10));
		    
		    
		    //matchs(String regex)       
		    try {
		      String regex = "1234";
		      System.out.println(regex.matches("//d{4}"));
		      System.out.println(regex.replaceAll("//d{4}", "chen"));
		      System.out.println(regex.replaceFirst("//d{4}", "chen"));
		      
		    } catch (PatternSyntaxException e) {
		      // TODO: handle exception
		      e.printStackTrace();
		    }
		    
		    // offsetByCodePoints(int index,int codePointOffset)
		    //      index   codePointOffset       
		      System.out.println(strCom.offsetByCodePoints(7, 4));
		    
		      //             ,      true        
		      System.out.println(strCom.regionMatches(true, 0, "I lIke", 0, 3));
		      System.out.println(strCom.regionMatches(0, "I like", 0, 3));
		      
		      //         ,      newChar              oldChar    。 
		      System.out.println(strCom.replace('A', 'd'));
		      System.out.println(strCom.replace("JAVA", "PHP"));
		      
		    //String[] split(String regex,int limit)
		      //                         ,limit         
		      System.out.println("----------------split-------------------");
		      String[] info = strCom.split(" ,");
		      for (int i = 0; i < info.length;i++)
		        System.out.println(info[i]);
		       
		      info    = strCom.split(" ", 2);
		      for (int i = 0; i < info.length;i++)
		        System.out.println(info[i]); 
		      
		      //startsWith(String prefix,int toffset)//           
		      //toffset             false
		      System.out.println(strCom.startsWith("I"));
		      System.out.println(strCom.startsWith("I",-1));
		      
		      
		    //CharSequeuece subSequeuece(int beginIndex,int endIndex)
		      //          
		      System.out.println(strCom.subSequence(2, 5));
		      
		    //String substring(int beginindex,int endIndex)
		      //      
		      System.out.println(strCom.substring(2));
		      System.out.println(strCom.substring(2, 6));
		      
		      //toCharArray()         
		      char[] str3 = strCom.toCharArray();
		      for (int i = 0; i < str3.length;i++)
		        System.out.print(str3[i]+" ");
		      System.out.println();
		      
		      //toLowerCase(Locale locale)              /         
		      System.out.println(strCom.toLowerCase());
		      System.out.println(strCom.toUpperCase());
		      System.out.println(strCom.toUpperCase(form));
		      System.out.println(strCom.toLowerCase(form));
		      
		      //trim()            
		      System.out.println(("    "+strCom).trim());
		      //valueOf()                       
		      
		      System.out.println("-----------valueOf()------------");
		      System.out.println(String.valueOf(true));
		      System.out.println(String.valueOf('A'));
		      System.out.println(String.valueOf(12.0));
		      
		  } 	
	 }