ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

常用类

2022-02-10 02:00:29  阅读:114  来源: 互联网

标签:index 常用 String point int public class


1,String类

/*
统计一个String类对象字符串,大写字母,小写字母,非字母出现的次数
 */
public class Test2 {
    public static void main(String[] args){
//        //方法一
//        String s = "abDJD, slkSDH,?.DK";
//        int countL = 0;
//        int countD = 0;
//        int countF = 0;
//        for(int i = 0;i<s.length();i++){
//            char c = s.charAt(i);
//            if(c>='a'&&c<='z'){
//                countL++;
//            }else if(c>='A'&&c<='Z'){
//                countD++;
//            }else{
//                countF++;
//            }
//        }
//        System.out.printf("小写字母出现的次数%d\t",countL);
//        System.out.printf("大写字母出现的次数%d\t",countD);
//        System.out.printf("非字母出现的次数%d\t",countF);
//        System.out.println("-----------------------");
//方法二 String str1 = "a、b、c、d、e、f、g、h、i、j、k、l、m、n、o、p、q、r、s、t、u、v、w、x、y、z"; String str2 = "A、B、C、D、E、F、G、H、I、J、K、L、M、N、O、P、Q、R、S、T、U、V、W、X、Y、Z"; String s = "abDJD, slkSDH,?.DK"; int countL = 0; int countD = 0; int countF = 0; for(int i = 0;i<s.length();i++){ char c = s.charAt(i); if(str1.indexOf(c)!=-1){ countL++; }else if(str2.indexOf(c)!=-1){ countD++; }else{ countF++; } } System.out.printf("小写字母出现的次数%d\t",countL); System.out.printf("大写字母出现的次数%d\t",countD); System.out.printf("非字母出现的次数%d\t",countF); } }
/*
    统一一个字符串在另一个字符串中出现的次数
*/

public class TestString4
{
    public static void main(String[] args)
    {
        String str1 = "abcasdabcabc";
        String str2 = "abc";
        int index = -1;
        int cnt = 0;

        index = str1.indexOf(str2);
        while (-1 != index)
        {
            ++cnt;
            index = str1.indexOf(str2, index+str2.length());
        }
        System.out.printf("%d\n", cnt);
    }
}

StringBuffer

 

 

 toString

 

 

public class TestToString {
    public static void main(String[] args){
        point p = new point(3,4);
        System.out.println(p);
    }
}
class point{
    int i,j;
    point(int i,int j){
        this.i = i;
        this.j = j;
    }
}

 

运行结果

 

 

public class TestToString {
    public static void main(String[] args){
        point p = new point(3,4);
        System.out.println(p);//默认System.out.println(p.toString)
    }
}
class point{
    int i,j;
    point(int i,int j){
        this.i = i;
        this.j = j;
    }
    public String toString(){//重写父类的方法
        return "["+i+","+j+"]";
    }
}

 

标签:index,常用,String,point,int,public,class
来源: https://www.cnblogs.com/18083122723qwer/p/15877599.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有