隨筆 - 19  文章 - 0  trackbacks - 0
          <2012年8月>
          2930311234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          第一:

          package com.javami.kudy.javaapi1.StringStudy;
          
          public class StringDemo {
              /*面試題:
               * 1.Stirng s = "abc" String s = new String("abc"); //有什么區別?
               * 2.String s = new String("abc"); 創建了幾個對象 2個(池里沒有)或1個(池里有了),
               * 3.String s = "a" + "b" + "c" + "d"; 創建了幾個對象? 創建了1個對象
               * 4.String s1 = "a"; String s2 = "b"; String s3 = s1 + s2;  s3=="ab"?
               */
              
              public static void main(String[]args)
              {
                  /*
                   * 1.String池,由于字符串操作很頻繁.java針對String對象提供了緩沖池
                   * s1首先會在緩沖池里面看一下有沒有"abc" 如果沒有的.那么把對象創建的引用返回給s1
                   * 當s2開始執行.會檢查緩沖池里面有沒有.如果有的.直接把對象的地址返回給s2 .所以是相等的.。
                  String s1 = "abc";
                  String s2 = "abc";
                  System.out.println(s1 == s2);
                  */
                  
                  /*
                  String s1 = "abc";
                  String s2 = new String(s1); 
                  outcome : false
                  s2在堆內存中創建了一個對象(把地址返回來~),并且在堆內存里面的緩沖池里面創建一個"abc"(這個是對象里面的一個拷貝副本)
                  做java的日常維護.所以那么這兩個地址一比較.就會不相等..
                  */
                  
                  /*
                  String s1 = "a"+"b"+"c";
                  String s2 ="abc";
                  outCome = "true";
                  因為java編譯器有個合并已知量的功能,在編譯階段"a"+"b"+"c" 合并為"abc"并且保存在緩沖池里面
                  所以s2在檢測緩沖池里面.檢測到已經有了.直接把對象的引用所返回來.那么證明了s1  s2 是同一個地址/所以比較出來為true
                  */
                  
                  /*
                  String s1 = new String("abc");
                  String s2 = new String("abc");
                  這兩個里面創建了多少個對象呢?
                  解答:  首先會在堆內存中創建一個對象.在檢測池里面有沒有,沒有也在了緩沖池里面創建了一個.做日常的維護
                  s2也會在堆內存中創建一個對象.這時候檢測到池里面已經有了.所以就不再創建/
                  說百了也就是3個..
                  一般面試簡單的就直接說:2個就可以.
                  */
                  
                  /*
                   * String 是一個常量,是不能改變的量
                   * 內部實現:
                   * StringBuilder sb = new StringBuilder(s1);
                   * sb.append(s2);
                   * s3 = sb.toString(); //轉換回字符串
                   * 返回調用方法的都是創建一個對象的
                   * 
                   */
                  String s1 = "a";
                  String s2 = "b";
                  String s3 = s1 + s2;
                  System.out.println(s3=="ab"); //直接返回一個flase
                  
              }
          
          }

          第二:

          package com.javami.kudy.javaapi1.StringStudy;
          
          import java.io.UnsupportedEncodingException;
          
          public class DecodeTest {
              
              /*
               * 解碼: 用錯誤的碼再變回來.然后用正確的編碼方式就可以
               * 編碼錯誤: 沒辦法,神仙也救不了
               */
              public static void main(String[]args) throws UnsupportedEncodingException
              {
                  String str1 = "中國人";  //使用gb2312編碼
                  // 使用平臺的默認字符集將此 String 編碼為 byte 序列,并將結果存儲到一個新的 byte 數組中。
                  byte[] newbyte = str1.getBytes();
                  
                  String str2 = new String(newbyte);
                  System.out.println(str2);
                  
                  
                  String str3 = new String(newbyte,"iso-8859-1");
                  //System.out.println(str3);編碼的信息出錯
                  newbyte = str3.getBytes("iso-8859-1");    
                  String str4 = new String(newbyte);
                  System.out.println(str4);
                  
                  byte[]new1 = str1.getBytes("iso-8859-1");
                  //一開始編碼的信息就出錯!因為我的計算機根本就沒有這個編碼
                  String s6 = new String(new1);
                  new1 = s6.getBytes("iso-8859-1");
                  String st7 = new String(new1);
                  System.out.println(st7);
              }
          }

          第三:

          package com.javami.kudy.javaapi1.StringStudy;
          
          public class StringTest {
              /*String 為什么要重寫toString()方法呢?
                  解答:因為在String如果不重寫Object的toString方法.那么直接復用父類的方法.打印出的是對象.
                  這顯然不是String想要得到的結果
               */
                  public static void main(String[]args) 
                  {
                      String s1 = "kudy";
                      System.out.println(s1.charAt(3)); //索引是從零開始\
                      String s2 ="abcde";
                      String s3 = "aBcddddd";
                      System.out.println(s2.compareToIgnoreCase(s3));
                      
                      String s4 = "abc";
                      String s5 = "def";
                      System.out.println((s4.concat(s5)).toString());
                      
                      boolean is = s4.contains(s5);
                      System.out.println(is);
                      
                      String fileName = "Demo.jdk";
                      if(fileName.endsWith(".java"))
                              System.out.println("您是java文件");
                      
                      String s6 = "cnabcdbvklabcjdhfabcdhla";
                      int newint = s6.indexOf("abc",0);  //注意:索引是從零開始
                      System.out.println(newint);
                      
                      String s7 = "abc";
                      String s8 = new String("abc");
                      s8.intern();
                      System.out.println(s7 == s8);
                      
                      String s9 = "cascascacvdfdjkgksd";
                      int num = s9.lastIndexOf("k",s9.length()-1);
                      System.out.println(num);
                      
                      String s10 = "abc";
                      String s11 = s10.replace("abc","***");
                      System.out.println(s11);
                      
                      String s12 = "hello.java";
                      String s13 = s12.replace("java","jdk" );
                      System.out.println(s13);
                      
                      
                      /*
                       * 把有逗號的內容去掉。
                       */
                      String s14 = "ab,cd,ef";
                      String [] s15 = s14.split(",");
                      for(String num1 : s15)
                              System.out.print(num1);
                      
                      String s16 = "abcbsbdasdas";
                      char[] newchar = s16.toCharArray();
                      for(char num2 : newchar)
                              System.out.print(num2);
                      System.out.println();
                      
                      /*
                       * 但注意中間的是不能去掉的~~
                       */
                      String s17 = "   abc      def     ";
                      String s18 = s17.trim();
                      System.out.println(s18);
                  }
          }

          第四:

            1 package com.javami.kudy.javaapi1.StringStudy;
            2 
            3 import java.io.IOException;
            4 
            5 public class StringUtil {
            6     private StringUtil(){} //私有化
            7     
            8     /* 查找子字符串在字符串中出現的位置 例:“ab”在"habjklabljhlabhjdjfla"出現的位置為:1 6  12 
            9      * 思路:
           10      * 1.定義一個StringBuilder容器,用于存儲位置,定義一個pos變量記住出現的位置,初始值為0
           11      * 2.定義一個while循環,查找子串出現的位置
           12      * 3.調用indexOf方法,從起始位置開始查找出子串首次出現的位置
           13      * 4.如果pos為-1,說明沒找到,應該結束循環
           14      * 5.將位置存入StringBuilder容器,再加一個逗號分隔
           15      * 6.從找到的位置加上子串的長度作為新的起始位置,
           16      * 7.循環結束,將StringBuffer轉為String字符串,調用split方法以逗號進行分隔,獲得了String數組
           17      * 8.準備一個新的int數組,長度和String數組一樣,遍歷String數組,將每個元素轉為int存入int數組
           18      */
           19     public static int[]findIndexOfSubstr(String str, String substr)
           20     {
           21         StringBuilder sb = new StringBuilder();
           22         int pos = 0;
           23         while(true)
           24         {
           25              pos = str.indexOf(substr,pos);
           26              if(pos ==-1)
           27                  break;
           28              sb.append(pos+",");
           29              pos+=substr.length(); //pos假設找ab 找到1 + 2 等于三.剛好從第三個開始找~
           30         }
           31         if(sb.length() == 0) //容器什么都沒有!
           32             return null;  //一個都找不到
           33         String s = sb.toString(); //轉換成字符串形式
           34         String[] parts = s.split(",");
           35         int[] arr = new int[parts.length];
           36         for(int i=0; i<parts.length; i++)
           37         {
           38             arr[i] = Integer.parseInt(parts[i]);
           39         }
           40         return arr;
           41     }
           42     
           43     /*
           44      * 設計一個方法,讀取鍵盤輸入的一行(只支持英文字母)
           45      * 提示:System.in.read()讀取鍵盤上的一個字符
           46      * 思路: 
           47      * 1.定義一個容器StringBuilder容器
           48      * 2.定義一個while循環,循環讀取鍵盤.
           49      * 3.如果讀到回車或者\n 就跳出循環
           50      * 4.存入StringBuilfer容器中
           51      * 5.循環結束,返回一個toString轉換為字符串
           52      */
           53         public static String readLine() throws IOException
           54         {
           55             StringBuilder sb = new StringBuilder();
           56             while(true)
           57             {
           58                 int ch = System.in.read();
           59                 if(ch == '\r')  //如果回車 跳出去
           60                     continue;
           61                 if(ch == '\n')  //等上面的執行完畢.再來
           62                     break;
           63                 sb.append((char)ch);
           64             }
           65             return sb.toString(); //轉換成字符串
           66         }
           67         
           68         /*
           69          * *將給定的字節數組截取一部分,進行解碼,要求輸出完整的漢字,例如:"abc中國人"截取6個 輸出的是abc中
           70          * 思路:
           71          *     1.遍歷數組被截取的部分,
           72          * 2.判斷正負數,統計負數個數為count
           73          * 3.判斷count的奇偶,奇數減去1,偶數不變
           74          * 4.解碼
           75          * 漢字是占了2個字節?我們怎么判斷它是漢字呢?
           76          * 漢字的第一位字節保證是負數!第二個是正數
           77          * 
           78          */
           79         public static String decode(byte[]buf, int len)
           80         {
           81             int count = 0;
           82             for(int i=0; i<buf.length;i++)
           83                 if(buf[i]<0)
           84                         count++; //如果第一個字節是負數的.是代表漢字馬上來到.咱們+1
           85             if(count % 2 == 1)
           86                 len--;
           87             return new String(buf,0,len);
           88             /*程序流程圖: 咱們輸入一個buf   len = 5
           89              * for 執行5次
           90              * 因為一個漢字是占了2個字節.而第一個字節保證是負數
           91              * 如果 buf[i]里面的是負數的 count +1 = 1
           92              * 如果   % 2  等于1代表的是:   咱們len 傳遞進來的就減去一個字節.
           93              * 
           94              */
           95         }
           96         
           97         public static String decodeBYGBK(byte[]buf,int len)
           98         {
           99             boolean b = false; 
          100             for(int i=0; i<buf.length; i++)
          101                 if(b)
          102                     b = false;
          103                 else if(buf[i]<0)
          104                     b = true;
          105             if(b)
          106                 len --;
          107             return new String(buf,0,len);
          108             
          109         }
          110         
          111         /*
          112          * 查找一個字符串出現次數最多的字符     abvjdabahabballfja   出現次數最多的為:a  6
          113          * 思路:
          114          * 1.調用toCharArray字符串把字符串轉換成字符數組
          115          * 2.定義max,用于記住出現最多字符的次數,定義一個maxChar記住出現最多的字符
          116          * 3.定義一個循環,每次循環都統計一個字符出現最多的次數,每次讓次數和max比.誰最大就換,結束條件。當長度的數組
          117          * 為零
          118          * 4.定義一個count,記住本次統計字符的次數
          119          * 5.循環遍歷數組,統計第一個字符出現的次數
          120          * 6.讓次數和max比,如果max大,讓max記住它的次數,讓maxchar記住當前的字符
          121          * 7.將當前的字符從數組中刪除
          122          * ------
          123          * 將此字符串轉換為一個新的字符數組。
          124          */
          125 
          126         public static char searchMaxChar(String str)
          127         {
          128             char[] buf = str.toCharArray();
          129             int max = 0;
          130             char maxChar = 0;
          131             while(buf.length>0)
          132             {
          133                 int count = 0; //個數等于0
          134                 char firstChar = buf[0];
          135                 for(char c : buf)
          136                     if(c ==firstChar)
          137                         count++;
          138                 if(count>max) //max 已經是默認記住上一個的值!
          139                 {
          140                     max = count;
          141                     maxChar = firstChar; //如果你是最多的就讓給你~~
          142                 }
          143                 //把當前的字符刪除!
          144                 buf = deleteChar(buf,firstChar,count);
          145             }
          146             return maxChar;
          147         }
          148 
          149         private static char[] deleteChar(char[] buf, char firstChar, int count)
          150         {
          151             char[]arr = new char[buf.length-count];
          152             int pos = 0;
          153             for(int i=0; i<buf.length; i++)
          154             {
          155                 if(buf[i]==firstChar)
          156                         continue;
          157                 arr[pos++] = buf[i];
          158             }
          159             return arr;
          160         }
          161         /* 查找兩個字符串中的最大子串  jflwertyakjfa;l    fjdlkwertyjf;ajf 最大子串:werty
          162          * 1.判斷出兩個字符串的長短,找出較長的和較短的
          163          * 2.循環遍歷較短的字符串的所有子串,由長到短遍歷
          164          *         定義一個循環,每次遍歷一種長度的子串,將i作為長度,i的取值: length~1
          165          *         定義一個內層循環,具體遍歷某一種長度的子串,j做為子串的開始位置, j取值:0~length-i
          166          *         截取子串,substring  起始位置:j  結束位置:j+i
          167          * 3.每次變量,都判斷子串是否為較長的字符串的子串,如果是,直接返回
          168          * 4.循環結束,依然沒有返回,說明沒有相同子串,返回null
          169          */
          170         public static String searchMaxSubStr(String s1, String s2) 
          171         {
          172             String longStr = s1.length() > s2.length() ? s1 : s2;
          173             String shortStr = s1.length() > s2.length() ? s2 : s1; 
          174             for(int i=shortStr.length(); i>0; i--)
          175             {
          176                 for(int j=0;j<shortStr.length()-i+1;j++)
          177                 {
          178                     String sub = shortStr.substring(j, j+i); //例如: abcd 放到sub里面去
          179                     //abcd abc ab  a 和它里面的做一個對比  
          180                     //當且僅當此字符串包含指定的 char 值序列時,返回 true。 
          181                     if(longStr.contains(sub)) 
          182                         //判斷長的字符串里面是否包含這個
          183                         return sub; //如果包含直接返回.沒有包含
          184                 }
          185             }
          186             return null;
          187         }
          188 }
          189 
          190 
          191 ----------------------
          192 
          193 package com.javami.kudy.javaapi1.StringStudy;
          194 
          195 public class StringUtilTest {
          196         public static void main(String[]args) throws Exception
          197         {
          198             /*
          199             String str = "dsdcvcvxvczxbedfaacff";
          200             String substr = "y";
          201             //遍歷a 出現的位置
          202             int[]arr =  StringUtil.findIndexOfSubstr(str, substr);
          203             if(arr ==null)
          204             {
          205                 System.out.println("找不到!");
          206                 return ;
          207             }
          208             for(int num : arr)
          209             {
          210                     System.out.print(num+" ");
          211             }
          212             System.out.println();
          213             
          214             */
          215             /*
          216              * 再輸入byte的時候就結束,不斷的去讀
          217 
          218             while(true)
          219             {
          220                 String str2 = StringUtil.readLine();
          221                 if("bye".equals(str2))
          222                     break;
          223                 System.out.println(str2);
          224             }
          225             System.out.println("程序結束");    
          226             */
          227             byte[] buf = "abc中國人".getBytes();
          228             //String s3 = StringUtil.decode(buf, 3);
          229             String s3 = StringUtil.decodeBYGBK(buf, 5);
          230             System.out.println(s3);
          231             
          232             char maxChar = StringUtil.searchMaxChar("dasfasdfaaaaaaaasfgdagfafgsad");
          233             System.out.println(maxChar);
          234             
          235             String s33 = "jflwertyakjfa;l";
          236             String s44 = "fjdlkwertyjf;ajf";
          237             String s55 =StringUtil.searchMaxSubStr(s33,s44);
          238             System.out.print(s55);
          239         }
          240 
          241 }
          posted on 2012-08-07 01:07 、小細 閱讀(247) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 明溪县| 麟游县| 宜丰县| 广南县| 巢湖市| 泌阳县| 永泰县| 广昌县| 雷山县| 凭祥市| 嘉义县| 东海县| 北辰区| 德保县| 大石桥市| 双流县| 临洮县| 博客| 鹿泉市| 民勤县| 彩票| 武定县| 嘉祥县| 成武县| 兖州市| 循化| 佛坪县| 奉节县| 奉化市| 隆德县| 青岛市| 台中县| 上犹县| 花莲市| 绥阳县| 七台河市| 武功县| 新源县| 渑池县| 江安县| 军事|