posts - 14,  comments - 7,  trackbacks - 0
                   java API 是應用編程接口,是一種規(guī)范,指明編寫應用程序的程序員如何訪問類和行為和狀態(tài)。
                   下面就列舉個API 中string類中的10個方法的小應用:
                  1.  contains:當且僅當此字符串包含指定的char值序列時,返回true
                   
          public class Contains {
                  
          public static void main(String[] args) {
                  String str 
          ="abcdefg";
                  
          boolean i = str.contains("ab");
                  System.out.print(i);
                  
              }


          }
                如果把上面程序中的 boolean i = str.contains("ab")改為 boolean i = str.contains(null) 即把指定的char值序列改為空值,便會出現(xiàn)NullPointExcption的異常。
               
                2.copyValueOf:通過這個方法把給定數(shù)組中的相應的部分copy到字符串中輸出來
          public static void main(String[] args) {
                  
          char[] x = {'a','b','c','d','e'};
                  String str 
          = null;
                  str 
          = String.copyValueOf(x,2,3);
                  System.out.print(str);

              }


          }
                  str = String.copyValueOf(x,2,3)其中的x表示原數(shù)組,2表示偏移量,3表示要輸出的長度。
           
                 3.Intern:返回字符串對象的規(guī)范化表示形式,遵循以下規(guī)則:對于任意兩個字符串s,t,當且僅當s.equals(t) 為true時,s.intern() == t.intern 才為true
          public static void main(String[] args) {
              String str1 
          = "HelloWorld";
              String str2 
          = "Hello"+"World";
              System.out.println(str1 
          == str2);
              System.out.print(str1.intern( ) 
          == str2.intern());
              }


          }
               4.length:返回此對象的長度
          public class Length {

              
              
          public static void main(String[] args) {
                  String str 
          = "abcdefg";
                  System.out.print(str.length());
              }


          }
                5.matches告知此字符串是否匹配指定的正則表達式,當且僅當匹配給定的正則表達式時,輸出true
          public class Matches {

              
              
          public static void main(String[] args) {
                  String str 
          = "abcdefg";
                  
          boolean i =str.matches( "abcdefg");
                  System.out.println(i);
              }


          }
                6.replaceFirst:用給定的字符串替換正則表達式匹配的字符串
          public class ReplaceFirst {
                  
          public static void main(String[] args) {
                  String str 
          = "abcdefgh";
                  String str1 
          = str.replaceFirst("ef""k");
                  System.out.print(str1);
              }


          }

               7.subString:返回一個新字符串,他是此字符串的一個字子符串,該子字符串從指定字符串索引處的字符開始,直到指定的索引結束的字符結束
          public class Substring {

              
              
          public static void main(String[] args) {
                  String str 
          = "abcdefgh";
                  String str1 
          = str.substring(24);
                  System.out.print(str1);
              }

          }

          其中String str = "str.substring(2,4)"中2表示起始索引(包括),4表示結束索引(不包括)
          如果把其實索引改為負數(shù)或結束索引大于字符串長度,便會出現(xiàn)indexoutBounsException的異常
                 8.toCharArray:將此字符串轉(zhuǎn)化為一個新的字符數(shù)組
          public class ToCharArray {
              
          public static void main(String[] args) {
                  String str1 
          = "一定要死幸福";
                  
          char[] str2 = str1.toCharArray( );
                  
          for(int i=0;i<str2.length;i++){
                  System.out.println(str2[i]);
                  }

              }

          }
                9.trim:此方法可用于截去開頭和尾部的空白
          public class Trim {
              
          public static void main(String[] args) {
              String str 
          = "  abcd   ";
              String strArr 
          = str.trim();
              System.out.println(strArr);
              }


          }
               10.valueOf:返回char參數(shù)的字符串表示形式
               
          public class ValueOf {
              
          public static void main(String[] args) {
              
          char[] x = {'a','b','c','d'};
              String str1 
          ="null";
              str1 
          = String.valueOf(x);
              System.out.print(str1);

              }


          }

          posted on 2010-10-09 17:48 迷人笑笑 閱讀(445) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          <2010年10月>
          262728293012
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          常用鏈接

          留言簿(13)

          隨筆檔案

          文章檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 吉木萨尔县| 嘉兴市| 宜宾县| 荆州市| 洛宁县| 龙井市| 贵德县| 旌德县| 辽阳县| 安顺市| 叶城县| 外汇| 建阳市| 广西| 青州市| 大渡口区| 循化| 桐乡市| 阿拉善左旗| 合川市| 襄城县| 额济纳旗| 辉县市| 米泉市| 新巴尔虎左旗| 黔江区| 恩施市| 喀喇沁旗| 大连市| 梁平县| 休宁县| 门头沟区| 浦北县| 五河县| 资中县| 噶尔县| 克什克腾旗| 四川省| 龙井市| 山东省| 新津县|