Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code> String line = "Hi. My name is John. Who are you ?"; String[] sentences = line.split("(?&lt;=[.!?])\\s+"); for (String sentence : sentences) { System.out.println("[" + sentence + "]"); } </code></pre> <p>This produces:</p> <pre><code>[Hi.] [My name is John.] [Who are you ?] </code></pre> <h3>See also</h3> <ul> <li><a href="http://www.regular-expressions.info/tutorial.html" rel="nofollow noreferrer">regular-expressions.info tutorials</a> <ul> <li><a href="http://www.regular-expressions.info/lookaround.html" rel="nofollow noreferrer">Lookarounds</a></li> <li><a href="http://www.regular-expressions.info/charclass.html" rel="nofollow noreferrer">Character classes</a></li> </ul></li> <li><a href="http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html" rel="nofollow noreferrer">Java language guide: the for-each loop</a></li> </ul> <hr> <p>If you're not comfortable using <code>split</code> (even though it's the recommended replacement for the "legacy" <a href="http://java.sun.com/javase/6/docs/api/java/util/StringTokenizer.html" rel="nofollow noreferrer"><code>java.util.StringTokenizer</code></a>), you can just use <em>only</em> <a href="http://java.sun.com/javase/6/docs/api/java/util/Scanner.html" rel="nofollow noreferrer"><code>java.util.Scanner</code></a> (which is more than adequate to do the job).</p> <h3>See also</h3> <ul> <li><a href="https://stackoverflow.com/questions/691184/scanner-vs-stringtokenizer-vs-string-split">Scanner vs. StringTokenizer vs. String.Split</a></li> </ul> <p>Here's a solution that uses <code>Scanner</code>, which by the way <code>implements Iterator&lt;String&gt;</code>. For extra instructional value, I'm also showing an example of using <a href="http://java.sun.com/javase/6/docs/api/java/lang/Iterable.html" rel="nofollow noreferrer"><code>java.lang.Iterable&lt;T&gt;</code></a> so that you can use the for-each construct.</p> <pre><code> final String text = "Hi. I am John.\n" + "My name is John. Who are you ?"; Iterable&lt;String&gt; sentences = new Iterable&lt;String&gt;() { @Override public Iterator&lt;String&gt; iterator() { return new Scanner(text).useDelimiter("\\s*[.!?]\\s*"); } }; for (String sentence : sentences) { System.out.println("[" + sentence + "]"); } </code></pre> <p>This prints:</p> <pre><code>[Hi] [I am John] [My name is John] [Who are you] </code></pre> <p>If this regex is still not what you want, then I recommend investing the time to educate yourself so you can take matters into your own hand.</p> <h3>See also</h3> <ul> <li><a href="https://stackoverflow.com/questions/1059127/what-is-the-iterable-interface-used-for">What is the Iterable interface used for?</a></li> <li><a href="https://stackoverflow.com/questions/839178/why-is-javas-iterator-not-an-iterable">Why is Java’s Iterator not an Iterable?</a></li> </ul> <hr> <p><em>Note</em>: the <code>final</code> modifier for the local variable <code>text</code> in the above snippet is a necessity. In an illustrative example, it makes for a concise code, but in your actual code you should refactor the anonymous class to its own named class and have it take <code>text</code> in the constructor.</p> <h3>See also</h3> <ul> <li><a href="https://stackoverflow.com/questions/714602/anonymous-vs-named-inner-classes-best-practices">Anonymous vs named inner classes? - best practices?</a></li> <li><a href="https://stackoverflow.com/questions/1299837/cannot-refer-to-a-non-final-variable-inside-an-inner-class-defined-in-a-different">Cannot refer to a non-final variable inside an inner class defined in a different method</a></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.
 

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