消除"\ u3000"Java中的错误 [英] Eliminate the "\u3000" error in java
问题描述
当我尝试编译Java文件时,编译器会说非法字符\ u3000",
When I try to compile a java file, the compiler said "illegal character \u3000",
搜索后,我发现它是 中日韩统一表意文字 中文韩文和日文的SPACE.我决定不编写手动删除特殊Java空格的代码,而是决定编写一个简单的搜索和删除Java文件以消除它.
after searching, I find it is CJK Unified Ideographs Chinese Korean and Japanese's SPACE. Instead of deleting the special SPACE manually, I decide to code a simple search-and-deleting java file to eliminate it.
但是它没有指出索引错误.那么如何编写代码来消除这种特殊的SPACE
However It doesnot point out the index error. So how to write a code to eliminate this special SPACE
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
import java.io.IOException;
import java.util.*;
public class BufferReadAFile {
public static void main(String[] args) {
//BufferedReader br = null;
String sCurrentLine;
String message = "";
try {
/*br = new BufferedReader(new FileReader("/Users/apple/Test/Instance1.java"));
while ((sCurrentLine = br.readLine()) != null) {
message += sCurrentLine;
}
*/
String content = new Scanner(new File("/Users/apple/Coding/Instance1.java")).useDelimiter("\\Z").next();
//System.out.println(content);
searchSubString(content.toCharArray(),"\\u3000".toCharArray());
} catch (IOException e) {
e.printStackTrace();
}
}
public static void searchSubString(char[] text, char[] ptrn) {
int i = 0, j = 0;
// pattern and text lengths
int ptrnLen = ptrn.length;
int txtLen = text.length;
// initialize new array and preprocess the pattern
int[] b = preProcessPattern(ptrn);
while (i < txtLen) {
while (j >= 0 && text[i] != ptrn[j]) {
j = b[j];
}
i++;
j++;
// a match is found
if (j == ptrnLen) {
System.out.println("found substring at index:" + (i - ptrnLen));
j = b[j];
}
}
}
public static int[] preProcessPattern(char[] ptrn) {
int i = 0, j = -1;
int ptrnLen = ptrn.length;
int[] b = new int[ptrnLen + 1];
b[i] = j;
while (i < ptrnLen) {
while (j >= 0 && ptrn[i] != ptrn[j]) {
// if there is mismatch consider the next widest border
// The borders to be examined are obtained in decreasing order from
// the values b[i], b[b[i]] etc.
j = b[j];
}
i++;
j++;
b[i] = j;
}
return b;
}
}
推荐答案
我不认为"\\ u3000"
是您想要的.您可以打印出字符串并自己查看内容.您应该改用"\ u3000"
.请注意单反斜杠.
I don't think "\\u3000"
is what you want. You can print out the string and see the content yourself. You should use "\u3000"
instead. Note the single back slash.
System.out.println("\\u3000"); // This prints out \u3000
System.out.println("\u3000"); // This prints out the CJK space
或者,您可以直接在 CheckEmpty
类中的 if
检查中直接使用实际的CJK空格字符.
Alternatively, you could just use the actual CJK space character directly as in one of the if
checks in your CheckEmpty
class.
这篇关于消除"\ u3000"Java中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!