在 JAVA 中,我的代码中可能包含哪些隐藏字符?
What hidden charactere could be on my code in JAVA?
我正在做 lex 分析器,但遇到了一些问题。从源代码中读取所有字符后,我将它们放入一个字符串中,然后逐个字符读取并进行适当的操作。最后,这会生成一个列表,其中包含语言标记、spaces、断线和...一个我无法识别并需要清理的该死的字符。
for (int i = 0; i < tokenList.size(); i++) {
// Remove Espacos
if (tokenList.get(i).getLexema().equals(" ")) {
tokenList.remove(i);
}
// Remove Strings Vazias
else if (tokenList.get(i).getLexema().length() == 0) {
print("ada");
tokenList.remove(i);
}
// Remove Tabulação
else if (tokenList.get(i).getLexema().equals("\t")) {
tokenList.remove(i);
}
// Remove Quebras de Linha
else if (tokenList.get(i).getLexema().equals("\n")) {
print("ASD");
tokenList.remove(i);
}
}
来自以下条目:
int a;
char n;
经过所有分析和清理,我得到以下结果:
00 - Lex: int
01 - Lex: a
02 - Lex: ;
03 - Lex:
04 - Lex: char
05 - Lex: n
06 - Lex: ;
有一个空的space,我不知道如何删除它。
解决方案:
好吧,那些人太棒了,我可以解决我的问题。解决方案,使用一些更好的编码策略:
for (int i = 0; i < tokenList.size(); i++) {
String lexema = tokenList.get(i).getLexema();
switch (lexema) {
case "":
tokenList.remove(i);
i = i - 1;
break;
// Remove Espacos
case " ":
tokenList.remove(i);
i = i - 1;
break;
// Remove Tabulações
case "\t":
tokenList.remove(i);
i = i - 1;
break;
// Remove Quebras de Linha
case "\n":
tokenList.remove(i);
i = i - 1; // DEIXAR SEM O BREAK
break;
// Remove Caractere Estranho
case "\r":
tokenList.remove(i);
i = i - 1;
break;
default:
break;
}
}
另一种更简单的解决方案是使用 Character.isWhitespace()
。所以你的代码可以像这样简单:
for (int i = 0; i < tokenList.size(); i++) {
String lexema = tokenList.get(i).getLexema();
char c = lexema.charAt(0);
if (Character.isWhitespace(c)) {
tokenList.remove(i);
i = i - 1;
}
}
我正在做 lex 分析器,但遇到了一些问题。从源代码中读取所有字符后,我将它们放入一个字符串中,然后逐个字符读取并进行适当的操作。最后,这会生成一个列表,其中包含语言标记、spaces、断线和...一个我无法识别并需要清理的该死的字符。
for (int i = 0; i < tokenList.size(); i++) {
// Remove Espacos
if (tokenList.get(i).getLexema().equals(" ")) {
tokenList.remove(i);
}
// Remove Strings Vazias
else if (tokenList.get(i).getLexema().length() == 0) {
print("ada");
tokenList.remove(i);
}
// Remove Tabulação
else if (tokenList.get(i).getLexema().equals("\t")) {
tokenList.remove(i);
}
// Remove Quebras de Linha
else if (tokenList.get(i).getLexema().equals("\n")) {
print("ASD");
tokenList.remove(i);
}
}
来自以下条目:
int a;
char n;
经过所有分析和清理,我得到以下结果:
00 - Lex: int
01 - Lex: a
02 - Lex: ;
03 - Lex:
04 - Lex: char
05 - Lex: n
06 - Lex: ;
有一个空的space,我不知道如何删除它。
解决方案:
好吧,那些人太棒了,我可以解决我的问题。解决方案,使用一些更好的编码策略:
for (int i = 0; i < tokenList.size(); i++) {
String lexema = tokenList.get(i).getLexema();
switch (lexema) {
case "":
tokenList.remove(i);
i = i - 1;
break;
// Remove Espacos
case " ":
tokenList.remove(i);
i = i - 1;
break;
// Remove Tabulações
case "\t":
tokenList.remove(i);
i = i - 1;
break;
// Remove Quebras de Linha
case "\n":
tokenList.remove(i);
i = i - 1; // DEIXAR SEM O BREAK
break;
// Remove Caractere Estranho
case "\r":
tokenList.remove(i);
i = i - 1;
break;
default:
break;
}
}
另一种更简单的解决方案是使用 Character.isWhitespace()
。所以你的代码可以像这样简单:
for (int i = 0; i < tokenList.size(); i++) {
String lexema = tokenList.get(i).getLexema();
char c = lexema.charAt(0);
if (Character.isWhitespace(c)) {
tokenList.remove(i);
i = i - 1;
}
}