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 菜园小鸟 阅读(212) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 

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

常用链接

留言簿(1)

随笔档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜