首页 > 编程语言 >Java移除注释

Java移除注释

时间:2022-08-30 15:24:53浏览次数:60  
标签:Java String indexOf po2 po1 注释 template 移除 sb

public class Comments {
    StringBuilder template;
    final String holder1 = "←";
    final String holder2 = "↓";
    final String holder3 = "↖";
    int quote = 1024;

    public Comments(String template) throws Exception {
        this.template = new StringBuilder(template);
        //这段代码可有可无,安全起见最好加上
        if (template.indexOf(holder1) > -1)
            throw new Exception("this file already have the placeholder1, please check your file before compile!!");
        if (template.indexOf(holder2) > -1)
            throw new Exception("this file already have the placeholder2, please check your file before compile!!");
        if (template.indexOf(holder3) > -1)
            throw new Exception("this file already have the placeholder3, please check your file before compile!!");
        //这段代码可有可无,安全起见最好加上
    }

    public StringBuilder analysis() {
        String[] str1 = new String[quote];
        escape(template,"\\",holder1,str1);
//----------------------------------------------------------------------------
        String[] str2 = new String[quote];
        replace(template,"\"",holder2,str2);
//----------------------------------------------------------------------------
        String[] str3 = new String[quote];
        replace(template,"\"",holder3,str3);
//----------------------------------------------------------------------------
        replaceA(template,"//","\n");
        replaceB(template,"/*","*/");
//----------------------------------------------------------------------------
        restore(template,str3,holder3);
        restore(template,str2,holder2);
        restore(template,str1,holder1);

        return template;
    }

    private void restore(StringBuilder sb,String[] from,String holder){
        for (int i = 0; i < from.length; i++) {
            String ch = holder+i;
            int po = sb.indexOf(ch);
            if(po > -1)
            sb.replace(po,po+ch.length(),from[i]);
        }
    }

    private void escape(StringBuilder sb,String ch,String holder,String[] restore){
        int po1 = sb.indexOf(ch);
        int po2 = po1+2;

        int index = 0;
        while(po1 != -1){
            restore[index] = sb.substring(po1, po2);
            sb.replace(po1,po2,holder+index);
            index++;
            po1 = sb.indexOf(ch);
            po2 = po1+2;
        }
    }

    private void replace(StringBuilder sb,String ch,String holder,String[] restore){
        int po1 = sb.indexOf(ch);
        int po2 = sb.indexOf(ch,po1+1)+1;

        int index = 0;
        while(po1 != -1 && po2 != -1){
            restore[index] = sb.substring(po1, po2);
            sb.replace(po1,po2,holder+index);
            index++;
            po1 = sb.indexOf(ch);
            po2 = sb.indexOf(ch,po1+1)+1;
        }
    }

    private void replaceA(StringBuilder sb,String from,String to){
        int po1 = sb.indexOf(from);
        int po2 = sb.indexOf(to,po1+1);

        while(po1 != -1 && po2 != -1){
            sb.replace(po1,po2,"");
            po1 = sb.indexOf(from);
            po2 = sb.indexOf(to,po1+1);
        }
    }

    private void replaceB(StringBuilder sb,String from,String to){
        int po1 = sb.indexOf(from);
        int po2 = sb.indexOf(to,po1+1)+2;

        while(po1 != -1 && po2 != -1){
            sb.replace(po1,po2,"");
            po1 = sb.indexOf(from);
            po2 = sb.indexOf(to,po1+1)+2;
        }
    }
}

 

标签:Java,String,indexOf,po2,po1,注释,template,移除,sb
From: https://www.cnblogs.com/laremehpe/p/16639410.html

相关文章

  • Java8 多线程及并行计算demo
    Java8多线程及并行计算demo #接口publicinterfaceRemoteLoader{Stringload();defaultvoiddelay(){try{Thread.sleep(1000L......
  • Javascript解构赋值
    Javascript解构赋值解构赋值语法是一种Javascript表达式。通过解构赋值可以将属性/值从对象/数组中取出,赋值给其他变量引用链接语法vara,b,rest;[a,b]=[......
  • 使用java处理字符串公式运算的方法
    在改进一个关于合同的项目时,有个需求,就是由于合同中非数据项的计算公式会根据年份而进行变更,而之前是将公式硬编码到系统中的,只要时间一变,系统就没法使用了,因此要求合同中......
  • java类加载过程
    https://blog.csdn.net/weixin_37766296/article/details/80545283 https://www.cnblogs.com/wangwudi/p/12327942.html 类的加载顺序ClassLoader中默认的加载顺序......
  • Java包与Import导入
    包的概念包是Java语言提供的一种确保类名唯一性的机制,是类的一种组织和管理方式、是一组功能相似或相关的类或接口的集合。一个完整的类名是包名+类名,在没有import导入的......
  • Java 对象和类, 变量类型,构造方法,创建对象,实例,源文件申明规则
    Java作为一种面向对象语言。支持以下基本概念:多态继承封装抽象类对象实例方法重载对象:对象是类的一个实例(对象不是找个女朋友),有状态和行为。例如,一条狗是一个......
  • Java常见错误总结
    1.Parameter0ofmethodmodifyResponseBodyGatewayFilterFactoryinorg.springframework.cloud.gateway.config.GatewayAutoConfigurationrequiredabeanoftype'o......
  • Java---泛型
    泛型出现的原因Java的泛型是在JDK1.5开始才加上的。在此之前的Java是没有泛型的。没有Java的泛型使用起来给人感觉非常的笨重,为了体会泛型带来的好处,来看看如果没有泛型......
  • JAVA进阶--static、工具类、单例、继承--2022年8月28日
    第一节 static静态关键字1、成员变量的分类和访问分别是什么样的?静态成员变量(有static修饰,属于类,加载一次,可以被共享访问)访问格式:类名.变量......
  • java Map实体内容遍历后重建 map 空指针处理
    javaMap实体内容遍历后重建map空指针处理Map<String,Object>map=newHashMap<>();//NullPointerExceptionMap<String,Object>map1=map.entrySet()......