0

这是我的输出:“四分和七年前......”

然而,预期的输入是:“四分和七年前……”(引号和‘四’之间有一个空格”。

如何保留前导空格?到目前为止,这是我的代码。

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");

 }
}
4

1 回答 1

0

Trim()返回字符串的副本,省略前导和尾随空格。

因此,如果您只需要删除尾随空格,请使用以下正则表达式。

String newstr = str.replaceAll("\\s*$","");
于 2012-04-12T02:53:50.730 回答