2016-03-04 65 views
1

useDelimiter將無法識別垂直條,但會識別其他字符。useDelimiter將無法識別垂直條

這不起作用:

scan.useDelimiter("|"); 

這並不工作:

scan.useDelimiter(","); 

休息代碼:

Scanner scan = new Scanner("12,d, |, f | "); 

// initialize the string delimiter 
scan.useDelimiter(","); 

// Printing the delimiter used 
System.out.println("The delimiter use is "+scan.delimiter()); 

// Printing the tokenized Strings 
while(scan.hasNext()){ 
    System.out.print(scan.next()); 
} 

// closing the scanner stream 
scan.close(); 

回答

5

你需要逃避豎線(管道字符)做;

scan.useDelimiter("\\|"); 
+0

或[pattern.quote(字符串或多個)](https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#quote%28java.lang .String%29)。 – OldCurmudgeon

+0

謝謝,就是這樣。 – PixelPusher