从文件中的字符串中读取java特殊字符
我正在制作一个从 java 文件读取代码并删除所有注释的程序,但是我在特殊字符方面遇到了一些问题,因为我假设 " 中的所有内容都是字符串的一部分
例如我在文件中编写了:
String my_string= " \" "
字符串在到达第二个 \ 时不会结束,那么我如何检测 \" 就好像它是单个字符一样
我遇到的另一个问题是当我有类似这样的
String my_string= ' “‘
如果我正在读第一个字符一方面,我怎么知道我已经达到了“显然这样的 if(char==' ' ') 不起作用”
I am making a program that reads code from a java file and removes all the comments, but I am having some problems with special character since I am assuming everything inside the " is part of a string
For example I have written in the file:
String my_string= " \" "
The string doesn't end when it reaches the second \, so how do I detect the \" as if it was a single character
Another issue I have is for when I have something like this
String my_string= ' " '
If I am reading the character one by one, how do I exactly I know I have reached an ' obviously something like this if(char==' ' ') doesn't work
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 if(char=='\'')
要检测转义引号,只需检查反斜杠即可。如果有反斜杠,请务必将下一个字符视为特殊字符,而不是要解析的字符。
Use
if(char=='\'')
And to detect escaped quotes, just put in a check for a backslash. If there is a backslash, do consider the next character to be a special character and not something to be parsed.