java string類方法深入解析_java

來源:互聯網
上載者:User

複製代碼 代碼如下:

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)匹配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()     靜態方法 實現基礎資料型別 (Elementary Data Type)轉成字串
    System.out.println(String.valueOf(true));
    System.out.println(String.valueOf('A'));
    System.out.println(String.valueOf(12.0));
  }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.