"From" https://segmentfault.com/q/1010000003885362
Under Windows, we press the key on the keyboard Enter
, actually entered is the carriage return and the newline two characters: \r\n
, ASCII code respectively is13和10
By default, the Scanner
input you get is not included in the return line. For example, ____one_two\r\n
if you type, it will only get the output from the ____one_two
inside (of course, it may have to be obtained multiple times next()
), automatically filter the carriage return line
But when we force a carriage return or change the behavior delimiter, it does not automatically filter the carriage return line break. For example, if you use a carriage return \n
as a delimiter, enter the above content ____one_two\r
, which is the reason for the return of length
12. You can use the following procedure to verify that you can find the last character of the string that gets to the ASCII code of 13, which indicates that it is a carriage return character \r
:
// enter ' ____one_two\r\n ' to test New Scanner (system.in); Scanner.usedelimiter ("\ n" = scanner.next (); // Print Gets the contents and its length, and the ASCII code of the last character SYSTEM.OUT.PRINTLN (str + ":" + str.length () + "(" + Str.codepointat (Str.length ()-1) + ")");
Workaround: Use \r\n
as a delimiter is normal
[To] Java: questions about the Usedelimiter () method of scanner