Java 我的标记似乎跳过了文本文件的前导空格
这是我的成果:八十七年前 然而,预期的投入是:四十七年前。。。引号和“四”之间有一个空格 如何保持前导空格?这是到目前为止我的代码Java 我的标记似乎跳过了文本文件的前导空格,java,whitespace,token,Java,Whitespace,Token,这是我的成果:八十七年前 然而,预期的投入是:四十七年前。。。引号和“四”之间有一个空格 如何保持前导空格?这是到目前为止我的代码 public void checkDocument(Reader in, InputStream input, Writer out) throws IOException { Scanner sc = new Scanner(input); TokenScanner tok=new TokenScanner(in); Token too; Set<Strin
public void checkDocument(Reader in, InputStream input, Writer out) throws IOException {
Scanner sc = new Scanner(input);
TokenScanner tok=new TokenScanner(in);
Token too;
Set<String> fileCor=new HashSet<String>();
while (tok.hasNext()){
too=tok.next();
System.out.println(too);
if (too.isWord()){
if (!(dict.isWord(too.toString()))){
int n=1;
fileCor=corr.getCorrections(too.toString());
List<String> sortedOptions=new LinkedList<String>(fileCor);
Collections.sort(sortedOptions);
System.out.println("The word: "+too.toString()+" is not in the dictionary. Please enter the number corresponding with the appropriate action:");
System.out.println("0: Ignore and continue");
System.out.println("1: Replace with another word");
for (int i=0;i<sortedOptions.size();i++){
n=n+i+1;
System.out.println((i+2)+": Replace with \""+sortedOptions.get(i)+"\"");
}
int choice=getNextInt(0, n, sc);
if (choice==0){
}
else if (choice==1){
out.write(getNextString(sc).trim());
}
else out.write(sortedOptions.get(choice-2).trim());
}
else out.write(too.toString().trim());
}
else {
out.write(too.toString());
}
}
System.out.println("Document completed");
}
}
返回字符串的副本,并省略前导和尾随空格
因此,如果只需要删除尾随空格,请使用下面的正则表达式
String newstr = str.replaceAll("\\s*$","");
返回字符串的副本,并省略前导和尾随空格。也许这就是问题所在。