Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One approach is to create a <code>StringReader</code> from the string, wrap it in a <code>BufferedReader</code> and use that to read lines. Alternatively, you could just split on <code>\n</code> to get the lines, of course...</p> <pre><code>String[] allLines = text.split("\n"); List&lt;String&gt; selectedLines = new ArrayList&lt;String&gt;(); for (int i = 0; i &lt; allLines.length; i += 100) { selectedLines.add(allLines[i]); } </code></pre> <p>This is simpler code than using a <code>BufferedReader</code>, but it does mean having the complete split string in memory (as well as the original, at least temporarily, of course). It's also less flexible in terms of being adapted to reading lines from other sources such as a file. But if it's all you need, it's pretty straightforward :)</p> <p>EDIT: If the start indexes are needed too, it becomes <em>slightly</em> more complicated... but not too bad. You probably want to encapsulate the "start and line" in a single class, but for the sake of brevity:</p> <pre><code>String[] allLines = text.split("\n"); List&lt;String&gt; selectedLines = new ArrayList&lt;String&gt;(); List&lt;Integer&gt; selectedIndexes = new ArrayList&lt;Integer&gt;(); int index = 0; for (int i = 0; i &lt; allLines.length; i++) { if (i % 100 == 0) { selectedLines.add(allLines[i]); selectedIndexes.add(index); } index += allLines[i].length + 1; // Add 1 for the trailing "\n" } </code></pre> <p>Of course given the start index and the line, you can get the end index just by adding the line length :)</p>
    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. This table or related slice is empty.
    1. 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