2025-02-10

Java代碼

  1. /*@src  https://eric-619.javaeye.com/blog/693636

     

  2. * java.lang.String
    類中常見的方法:(字符串處理常用的方法)
     
  3. * 1.參數不嚴
    格 ;    2,字符串支持正則表達式
     
  4.  
  5. * 1、length():  
  6. *     制定一個字符串的
    長度;
     
  7. *      
  8. * 2、
    substring(int begin)
     
  9. *    substring(int beginindex, int endindex)

     

  10. *     substring(),
    參數從零開始數字符串;
     
  11. *       
  12. * 3、
    split(String s1)
     
  13. *    split(String s2, int i)
     
  14.  
  15. * 4、
    startsWith(String s1)
     
  16. *    startsWith(String s2, int i)
     
  17. *    endsWith(String s3)
     
  18.  
  19. * 5、
    replace(String sb, String st)
     
  20. *    replaceAll(String sb, String st)

     

  21.  
  22. */  
  23. public class TestString {

      

  24.   
  25.     public static void main(String[] args){

      

  26.    new TestString().testLength();

      

  27.    new TestString().testSubstring();

      

  28.    new TestString().testSplit();

      

  29.    new TestString().testStartsendsWith();

      

  30.    new TestString().testReplaceAll();

      

  31. }   
  32.        
  33.     
      
  34.     // 1、
    length():
      
  35.     public void testLength(){

      

  36.     String slen = “abc”;   
  37.     String slenn = new String(“abcdefg”);   
  38.     int i = slen.length();
      
  39.     int k = slenn.length();   
  40.     System.out.println(i);
      
  41.     System.out.println(k);   
  42.     System.out.println(“—————————–“);
      
  43.     }   
  44.        
  45.     
      
  46.     // 2、
    substring(int begin)、substring(int beginindex, int endindex)
      
  47.     // 註
    意:字符串從零開始數
      
  48.     public void testSubstring(){

      

  49.     String se = new String(“abcdefgh”);   
  50.     String sea = se.substring(3);   
  51.     String seaa = se.substring(3,5);   
  52.     System.out.println(sea);
      
  53.     System.out.println(seaa);   
  54.     System.out.

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *