자바 string 클래스 방법 깊이 분석

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

import javax.xml.crypto.Data;

public class Stringxuexi {
  public static void main(String[] argc)
  {
    //charAt(int index) index Unicode
    String strCom = "JAVA ";
    System.out.println(strCom.charAt(4));

    //codePointAt(int index) index Unicode
    strCom = "I like JAVA ,too";
    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"));

    //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("JAVA"));

    //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(0, 10, dst, 0);
    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
    // 。
    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));

    //
    System.out.println(strCom.length());

    //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));

    System.out.println(strCom.replace('A', 'a'));
    System.out.println(strCom.replace("JAVA", "PHP"));

    //String[] split(String regex,int limit)
    // ,limit
    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, 6));

    //String substring(int beginindex,int endIndex)
    //
    System.out.println(strCom.substring(2));
    System.out.println(strCom.substring(2, 6));

    //toCharArray()
    char[] str1 = strCom.toCharArray();
    for (int i = 0; i < str1.length;i++)
      System.out.print(str1[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(String.valueOf(true));
    System.out.println(String.valueOf('A'));
    System.out.println(String.valueOf(12.0));
  }
}

좋은 웹페이지 즐겨찾기