三种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. hdu2612 Find a way

    Problem Description Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. L ...

  2. ACM 字母统计

    字母统计 时间限制:3000 ms  |  内存限制:65535 KB 难度:1   描述 现在给你一个由小写字母组成字符串,要你找出字符串中出现次数最多的字母,如果出现次数最多字母有多个那么输出最小 ...

  3. Codeforces Round #228 (Div. 2) A. Fox and Number Game

    #include <iostream> #include <algorithm> #include <vector> #include <numeric> ...

  4. cdoj 1328 卿学姐与诡异村庄 Label:并查集 || 二分图染色

    卿学姐与诡异村庄 Time Limit: 4500/1500MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit  ...

  5. transform应用详解

    关于css3的transform,做了一个demo,上代码 html: <!DOCTYPE html> <html> <head lang="en"& ...

  6. C#_简单实用的翻页

    简单实用的生成翻页HTML辅助类 C# using System.Text; namespace ClassLibrary { /// <summary> /// /// </sum ...

  7. e.Handled的理解

    private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)         {    ...

  8. Mobile data

    1.Consume REST web services from app 2.De-serialize JSON into an in-memory object collection 3.Save ...

  9. LeetCode(43. Multiply Strings)

    题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...

  10. getElementsByClassName的兼容性

    /*----------------------------index.html------------------------------------*/ <!DOCTYPE html> ...