三种comments:

 /* Test program */ 

    int main()  

    {           

       // variable declaration 

       int a, b, c;    

       /* This is a test  

           multiline     

           comment for   

           testing */      

       a = b + c;       

    } 
 import java.util.*;
public class removeComments{
public static void main(String [] args){
String file = " /* Test program */ \n" +
" int main() \n" +
" { \n" +
" // variable declaration \n" +
" int a, b, c; \n" +
" /* This is a test \n" +
" multiline \n" +
" comment for \n" +
" testing */ \n" +
" a = b + c; \n" +
" } \n";
System.out.println(file);
System.out.println(removeComments(file));
} private static String removeComments(String s){
if(s == null || s.length() == 0){
return s;
}
StringBuilder res = new StringBuilder();
boolean sCom = false;
boolean mCom = false; for(int i = 0; i<s.length(); i++){
// System.out.println("i is " + i + "sCom, mCom = " + sCom + mCom);
if(sCom && (s.charAt(i) == '\n')){
System.out.println("i is " + i);
sCom = false;
}else if(mCom && s.charAt(i) == '*' && i+1<s.length() && s.charAt(i+1) == '/'){
mCom = false;
i++;
}else if(sCom || mCom){
continue;
}else if(s.charAt(i) == '/' && i+1 < s.length() && s.charAt(i+1) == '/'){
sCom = true;
i++;
}else if(s.charAt(i) == '/' && i+1 <s.length() && s.charAt(i+1) == '*'){
mCom = true;
i++;
}else{
res.append(s.charAt(i));
}
}
return res.toString();
} }

去掉comments的更多相关文章

  1. 去掉VC2010 编辑器里出现的红色波浪线

    在VC2010中浏览代码的时候就大片的红线看着不舒服 其实不关VS的事,原因在于visual assist.   在VAssistX菜单栏->Visual Assist X Options-&g ...

  2. [从jQuery看JavaScript]-注释(comments)

    jQuery片段: /*! * jQuery JavaScript Library v1.3.2 * http://jquery.com/ * * Copyright (c) 2009 John Re ...

  3. 将VS2010里的红色波浪线去掉

    有时候,程序编译没错,却出现了一堆的红色波浪形,看着就烦. 解决方案:在VAssistX菜单栏->Visual Assist X Options->展开Advanced->Under ...

  4. spellchecker inspection helps locate typeos and misspelling in your code, comments and literals, and fix them in one click

    项目layout文件中出现 spellchecker inspection helps locate typos and misspelling in your code, comments and ...

  5. 去掉vs2010字符串下红色波浪线

    由于在vs集成了qt库,无法提升代码. 所以下载了visual assist,然后新的问题出现了,凡是在vs中输入的字符串,下面都有红色的波浪线,而且没有错误,只是看着不舒服. 解决方法: 在VAss ...

  6. 最新 去掉 Chrome 新标签页的8个缩略图

    chrome的新标签页的8个缩略图实在让人不爽,网上找了一些去掉这个略缩图的方法,其中很多已经失效.不过其中一个插件虽然按照原来的方法已经不能用了,但是稍微变通一下仍然是可以用的(本方法于2017.1 ...

  7. wget 显示"英国中部时间",去掉烦人的刷屏显示

    wget下载文件显示多行,进度条后面显示英国中部时间,非常让人郁闷. 本来英文是eta(Estimated Time of Arrival 预计到达时间),翻译错了,干脆去掉好了. 先要有两个个工具 ...

  8. 代码的坏味道(13)——过多的注释(Comments)

    坏味道--过多的注释(Comments) 特征 注释本身并不是坏事.但是常常有这样的情况:一段代码中出现长长的注释,而它之所以存在,是因为代码很糟糕. 问题原因 注释的作者意识到自己的代码不直观或不明 ...

  9. String 中去掉空格

    JAVA中去掉空格 1. String.trim() trim()是去掉首尾空格 2.str.replace(" ", ""); 去掉所有空格,包括首尾.中间 ...

随机推荐

  1. HTML5与移动端web学习笔记

    HTML5 提供了很多新的功能,主要有: 新的 HTML 元素,例如 section, nav, header, footer, article 等 用于绘画的 Canvas 元素 用于多媒体播放的 ...

  2. [深入浅出WP8.1(Runtime)]生成图片和存储生成的图片文件

    7.2.3 使用RenderTargetBitmap类生成图片 RenderTargetBitmap类可以将可视化对象转换为位图,也就是说它可以将任意的UIElement以位图的形式呈现.那么我们在实 ...

  3. Android --RatingBar的使用

    1.效果图

  4. not only ... but also

    轉載自http://210.240.55.2/~t311/moe/engb6/b6grammar/b6notonly.htm not only ... but also ... 是「不僅‧‧‧也是‧‧ ...

  5. NHibernate one-to-one

    NHibernate里面one-to-one有两种方式:主键关联和唯一外健关联 主键关联: 两个表拥有相同的主键字段,值相同的关联在一起.典型的应用是一个对象的属性太多,将常用的属性跟不常用的附加属性 ...

  6. LVS + KEEPAlived 配置 DIR模式

    1 .1 Lvs LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统.本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项目 ...

  7. linux中,常用的账号管理命令

    创建新用户:adduser 用户名创建新用户并将其加入一个现有组中:adduser 用户名 -G 组名创建新用户并使其只属于该组:adduser 用户名 -g 组名创建用户密码:passwd 用户名创 ...

  8. css3很酷的加载动画多款

    在线实例:http://www.admin10000.com/document/3601.html 源码:https://github.com/tobiasahlin/SpinKit

  9. get_magic_quotes_gpc()

    php get_magic_quotes_gpc()函数:http://www.cnblogs.com/lsk/archive/2008/05/05/1184117.html 以及addslashes ...

  10. javascript 数组的深度复制

    javascript 数组的深度复制 一般情况下,使用 "=" 可以实现赋值.但对于数组.对象.函数等这些引用类型的数据,这个符号就不好使了. 1. 数组的简单复制 1.1 简单遍 ...