美文网首页java基础学习
寻找子字符串/按行读文件

寻找子字符串/按行读文件

作者: 迷糊银儿 | 来源:发表于2020-03-21 22:45 被阅读0次
  1. 问题一 from sg
    其功能为从字符串str中返回从beginStr开始到endStr结束的中间子串。如str=”abcdefmsgleisw” beginStr=”cd” endStr=”ei” 结果为 “efmsgl“
public static String substr(String str, String begin, String end){
        if(str.length()<=0){
            return null;
        }
        int i= str.indexOf(begin);
        //System.out.println(i);

        int j= str.indexOf(end);
        if(i==-1 || j==-1 || j<=i){  // begin/end不存在 || end在begin之前
            return null;
        }
        i=i+begin.length();
        return str.substring(i, j);
    }
  1. 问题二 from sg


    image.png
public static class Pro{
        int  count;
        float price;
        public Pro(int  count,float price){
            this.count=count;
            this.price=price;
        }
    }

    public static void getAvgPrice() throws Exception{
        String path="log.txt";
        HashMap<String,Pro> hashMap=new HashMap<>();
        FileInputStream fileInputStream=new FileInputStream(path);
        InputStreamReader in=new InputStreamReader(fileInputStream,"UTF-8");
        BufferedReader bf=new BufferedReader(in);
        try {
            String content=bf.readLine();
            while (content!=null){
                float price=Float.parseFloat(content.split(" ")[4]);
                String name=content.split(" ")[3];
                if(hashMap.get(name)!=null){
                    hashMap.get(name).price+=price;
                    hashMap.get(name).count++;
                }else if(hashMap.get(name)==null){
                    hashMap.put(name,new Pro(1,price));
                }
                content=bf.readLine();
            }
            for(String key:hashMap.keySet()){
                System.out.println(key+" avg price is:"+hashMap.get(key).price/hashMap.get(key).count);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            fileInputStream.close();
            in.close();
            bf.close();
        }
    }
  1. 问题三 from ks
// 数组中每走m步,删除一个元素,数组走到最后则再回到数组首部,直到剩下最后一个元素输出;
    public static void delItem(){
        int[] arr={1,2,3,4,5,6};int m=2;
        int len=arr.length;
       //for(int i=0;i<len-1;i++){ //要找出len-1个元素,下标从0开始则i<len-1
        int i=0;
        int k=0;
        int j=0;
        while (i<len-1){
            if(k==m){
                arr[j%len]=-1;
                i++;
                k=0;
                System.out.printf(j%len+"");
            }
            if(arr[(++j)%len]!=-1) {
                k++;
            }
        }
        System.out.printf("\n");
        for (int f:arr){
            System.out.println(f);
        }
    }

相关文章

  • 寻找子字符串/按行读文件

    问题一 from sg其功能为从字符串str中返回从beginStr开始到endStr结束的中间子串。如str=”...

  • txt读写

    文件打开 读文件 读取字符串 按行读取整个文件 写文件 字符串写入txt 列表写入文件 双层列表写入文件 数组写入文件

  • Python IO 流

    转载请注明出处 读文件 读取整个文件 分段读取 按行读取代码 按行读取 二进制读取 写文件 文本写出 追加文件 二...

  • linux查找日志

    1、查找包含某字符串的行 2、统计包含某字符串的行数 3、从文件末尾开始查找包含某字符串的行限制100行 4、按文...

  • GO语言最简单的读写文件demo

    读文件 将整个文件的内容读到一个字符串里 按列读取文件中的数据 写文件 文件拷贝

  • 标准库使用示例

    执行系统命令 字符串首字母大写 文件按行读取 获取命令行参数

  • 文件操作

    一、文件操作-读 二、文件操作-写 三、复制文件 四、合并文件 合并文件-结果 五、readline按行读取文件、...

  • Python six day

    文件复制1 大文件处理方式readline()读一行 输入是字符串readlines()一行一行的都去完 输出是列...

  • Linux shell按行读文件

    写在前面 这里采用的测试文件的内容如下: 另外,这里的脚本都会放在test.sh中运行,当然,我不说你也可以看出来...

  • 第十四章 Caché 函数大全 $EXTRACT 函数

    第十四章 Caché 函数大全 $EXTRACT 函数 按位置从字符串中提取子字符串,或按位置替换子字符串。 大纲...

网友评论

    本文标题:寻找子字符串/按行读文件

    本文链接:https://www.haomeiwen.com/subject/vjspyhtx.html