posts - 3,  comments - 7,  trackbacks - 0
          public class TestString
          {
              
          public static void main(String[] args)
              
          {
                  
          //求該字符串包含多少個java子字符串
                  String s1 = "java,java_sunjavaworld,i love java,ha,6iy%$javaw87%^sn2^*java";
                  
          int num = gen(s1,"java");
                  System.out.println(
          "s1字符串一共包含"+num+"個子串");
                  

                  
          //求該字符串中包含多少個大寫字符、小寫字符、數字、其他字符
                  String s2 = "hdHoinlG&dd3NZW3OIN2,dsou5WE.!72Hsn__@#m";
                  fun1(s2); 
          //查找方法1
                  fun2(s2); //查找方法2
                  fun3(s2); //查找方法3
              }

              
          public static int gen(String str,String key)
              
          {
                  String s 
          = str;
                  
          int count = 0;
                  
          int position = 0;

                  
          while( (position = s.indexOf(key)) != -1)
                  
          {
                      count
          ++;
                      s 
          = s.substring(position+key.length());
                  }

                  
          return count;
              }

              
          public static void fun1(String s)
              
          {
                  
          int uCase = 0;
                  
          int lCase = 0;
                  
          int nCase = 0;
                  
          int oCase = 0;

                  
          for (int i = 0;i < s.length();i++)
                  
          {
                      
          char c = s.charAt(i);
                      
          if (c >= 'A' && c <= 'Z')
                      
          {
                          uCase
          ++;
                      }
          else if (c >= 'a' && c <= 'z')
                      
          {
                          lCase
          ++;
                      }

                      
          else if (c >= '0' && c <= '9')
                      
          {
                          nCase
          ++;
                      }

                      
          else
                      
          {
                          oCase
          ++;
                      }

                  }

                  System.out.println(
          "fun1.大寫字符:"+uCase+" 小寫字符:"+lCase+" 數字"+nCase+" 其他字符:"+oCase+" 總計:"+s.length());
              }

              
          public static void fun2(String s)
              
          {
                  
          int uCase = 0;
                  
          int lCase = 0;
                  
          int nCase = 0;
                  
          int oCase = 0;

                  String uS 
          = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                  String lS 
          = "abcdefghijklmnopqrstuvwxyz";
                  String nS 
          = "0123456789";

                  
          for (int i=0;i<s.length();i++)
                  
          {
                      
          char c = s.charAt(i);
                      
          if (uS.indexOf(c) != -1)
                      
          {
                          uCase
          ++;
                      }
          else if (lS.indexOf(c) != -1)
                      
          {
                          lCase
          ++;
                      }
          else if (nS.indexOf(c) != -1)
                      
          {
                          nCase
          ++;
                      }

                      
          else
                      
          {
                          oCase
          ++;
                      }

                  }

                  System.out.println(
          "fun2.大寫字符:"+uCase+" 小寫字符:"+lCase+" 數字"+nCase+" 其他字符:"+oCase+" 總計:"+s.length());
              }

              
          public static void fun3(String s)
              
          {
                  
          int uCase = 0;
                  
          int lCase = 0;
                  
          int nCase = 0;
                  
          int oCase = 0;

                  
          for (int i=0;i<s.length();i++)
                  
          {
                      
          char c =  s.charAt(i);
                      
          if (Character.isUpperCase(c))
                      
          {
                          uCase
          ++;
                      }
          else if (Character.isLowerCase(c))
                      
          {
                          lCase
          ++;
                      }
          else if (Character.isDigit(c))
                      
          {
                          nCase
          ++;
                      }
          else
                      
          {
                          oCase
          ++;
                      }

                  }

                  System.out.println(
          "fun3.大寫字符:"+uCase+" 小寫字符:"+lCase+" 數字"+nCase+" 其他字符:"+oCase+" 總計:"+s.length());
              }

          }

          //s1字符串一共包含6個子串
          //fun1.大寫字符:11 小寫字符:15 數字6 其他字符:8 總計:40
          //fun2.大寫字符:11 小寫字符:15 數字6 其他字符:8 總計:40
          //fun3.大寫字符:11 小寫字符:15 數字6 其他字符:8 總計:40
          posted on 2008-02-12 21:41 菜園小鳥 閱讀(224) 評論(0)  編輯  收藏

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


          網站導航:
           

          <2008年2月>
          272829303112
          3456789
          10111213141516
          17181920212223
          2425262728291
          2345678

          常用鏈接

          留言簿(1)

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 深圳市| 修文县| 佛学| 枞阳县| 五家渠市| 射洪县| 青铜峡市| 南城县| 南漳县| 曲周县| 成武县| 蒙山县| 丰顺县| 镇原县| 奉节县| 开远市| 泽库县| 沅江市| 卢氏县| 四子王旗| 花莲县| 新津县| 白山市| 察隅县| 青阳县| 紫云| 蒙城县| 美姑县| 锦州市| 喜德县| 石河子市| 当阳市| 江安县| 湟源县| 葵青区| 凉山| 平山县| 新乐市| 科技| 呼和浩特市| 巩义市|