全栈学习之后端基础 String类

笔记转载一个博客

原文地址原文地址

代码示例

method1代表几个小题
1、从abcdefg中获取1-4位字符串
2、检查abcdefg中没有bcd
3、将形如a,b,c,e,最后一个,去掉
4、替换abcedfgaaa中ab替换成bcd
剩下三个方法代表三个大题
1、给定一个由数字组成的字符串,如:“1239586838923173478943890234092”,统计出每个数字出现的次数
2、给定一个字符串,判断该字符串中是否包含某个子串.如果包含,求出子串的所有出现位置.
如:"abcd23abc34bcd"中,"bc"子串的出现位置为: 1,7,11.字符串和子串均由用户输入
3、给定一个字符串,请输出该字符串由哪些字符组成,每个字符出现几次?

public class Test {
    
    
    public static void main(String[] args) {
    
    
        Scanner s= new Scanner(System.in);
        Test test = new Test();
        //test.method1();
        //统计数字出现的次数
        System.out.println("请输入一串数字:");
        String number = s.next();
        test.count(number);
        //查找子串出现的位置
        System.out.println("请输入母串:");
        String parent = s.next();
        System.out.println("请输入子串:");
        String child = s.next();
        test.search(parent,child);
        //给定一个字符串,请输出该字符串由哪些字符组成,每个字符出现几次?
        System.out.println("请输入一个字符串:");
        String string = s.next();
        test.method_noname(string);
    }
    public void method_noname(String string){
    
    
        char c_arr[]=new char[100];
        int c_index = 0;
        int n_arr[]=new int[100];
        int n_index = 0;
        while (!string.equals("")){
    
    
            int count = 0;
            int index = -1;
            c_arr[c_index]  = string.charAt(0);
            count++;
            index = string.indexOf(c_arr[c_index]+"",1);
            while (index!=-1){
    
    
                count++;
                index = string.indexOf(c_arr[c_index]+"",index+1);
            }
            n_arr[n_index++] = count;
            string = string.replaceAll(c_arr[c_index]+"","");
            c_index++;
        }

        for (int i = 0; i < c_index; i++) {
    
    
            System.out.println(c_arr[i]+"出现的次数为:"+n_arr[i]);
        }
    }
    public void search(String parent,String child){
    
    
        int index = -1;
        int arr[] = new int[100];
        int i =0;
        index = parent.indexOf(child+"",0);
        while (index != -1){
    
    
            arr[i++] = index;
            index = parent.indexOf(child+"",index+1);
        }
        System.out.print(child+"出现的位置有:");
        for (int j = 0; j < i; j++) {
    
    
            System.out.print(arr[j]+" ");
        }
        System.out.println();
    }
    public void count(String tmp){
    
    
        int count = 0;
        int index = -1;
        int result[] = new int[10];
        for (int i = 0; i < 10; i++) {
    
    
            index = tmp.indexOf(i+"",0);
            //System.out.println(index);
            while (index != -1){
    
    
                count++;
                index = tmp.indexOf(i+"",index+1);
            }
            result[i]=count;
            count = 0;
        }
        for (int i = 0; i < result.length; i++) {
    
    
            if(result[i]>0){
    
    
                System.out.print(i+"出现的次数:"+result[i]+" ");
            }
        }
    }
    public void method1(){
    
    
        String s = "abcdefg";
        System.out.println("s:"+s);
        //截取前四位
        System.out.println(s.substring(0,4));
        //判断是否有bcd
        if(s.indexOf("bcd")!=-1){
    
    
            System.out.println("字符串中有bcd");
        }
        s="abcdefgaaa";
        System.out.println("s:"+s);
        //替换
        s = s.replaceAll("ab","bcd");
        System.out.println("s:"+s);
        //去掉最后一个逗号
        s="a,b,c,e,";
        System.out.println("s:"+s);
        s = s.substring(0,s.lastIndexOf(","));
        System.out.println("s:"+s);
    }
}

猜你喜欢

转载自blog.csdn.net/Walker7143/article/details/105986364
今日推荐