Find single line comments in byte array

房东的猫 提交于 2019-12-14 03:37:18

问题


Is it possible to find instances of // in a line read from a file into a byte array and then "snip" from // to the end of the line out? I'm trying

FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[8 * 1024];
int read;

while ((read = fis.read(buffer)) != -1) 
{
    for (int i = 0; i < read; i++) 
    {
        if (buffer[i] == '//')
        {
            buffer = buffer[0:i];
        }
    }
}

but I'm getting Invalid character constant at if (buffer[i] == '//') on the '//' part. Am I doing something wrong, or is this just not possible?


回答1:


Old-school solution

for (int i = 0; i < read-1; i++) 
    {
        (if (buffer[i] == '/') && (buffer[i+1]== '/'))
        {
            buffer = buffer[0:i];
        }
    }



回答2:


' and ' denote one character. Since // are two characters this does not work. One has to differentiate between a character and a string. Thus you have to individually check both positions in the byte array to confirm there are two successive /s.



来源:https://stackoverflow.com/questions/34420823/find-single-line-comments-in-byte-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!