Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your <code>useDelimiter()</code> and <code>next()</code> combo is faulty. <code>useDelimiter("")</code> will return 1-length substring on <code>next()</code>, because an empty string does in fact sit between every two characters.</p> <p>That is, because <code>"\r\n".equals("\r" + "" + "\n")</code> so <code>"\r\n"</code> are in fact two tokens, <code>"\r"</code> and <code>"\n"</code>, delimited by <code>""</code>.</p> <p>To get the <code>Matcher</code>-behavior, you need <code>findWithinHorizon</code>, which ignores delimiters.</p> <pre><code> Pattern newLinePattern = Pattern.compile("(\\r\\n?|\\n)", Pattern.MULTILINE); String sourceString = "\r\n\n\r\r\n\n"; Scanner scan = new Scanner(sourceString); int count = 0; while (scan.findWithinHorizon(newLinePattern, 0) != null) { count++; } System.out.println("found "+count+" newlines"); // finds 5 newlines </code></pre> <h3>API links</h3> <ul> <li><a href="http://java.sun.com/javase/6/docs/api/java/util/Scanner.html#findWithinHorizon%28java.util.regex.Pattern,%20int%29" rel="nofollow noreferrer"><code>findWithinHorizon(Pattern pattern, int horizon)</code></a> <blockquote> <p>Attempts to find the next occurrence of the specified pattern [...] ignoring delimiters [...] If no such pattern is detected then the <code>null</code> is returned [...] If <code>horizon</code> is 0, then [...] this method continues to search through the input looking for the specified pattern without bound.</p> </blockquote></li> </ul> <h3>Related questions</h3> <ul> <li><a href="https://stackoverflow.com/questions/2597841/scanner-method-to-get-a-char">Scanner method to get a char</a> <ul> <li><code>useDelimiter("")</code> will tokenize into 1-length substrings</li> </ul></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload