Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can do this with a single split (Well, this may not be most efficient at runtime, but this is concise, lesser code to write):</p> <pre><code>String[] arr = str.split("(?&lt;=\\G..)"); </code></pre> <p>And then get the <code>List&lt;String&gt;</code> using <code>Arrays#asList()</code> method. </p> <p>The regex pattern splits on an empty space preceded by 2 characters - <code>..</code>, but ignoring the character already considered in previous match - <code>\\G</code>. The anchor <code>\\G</code> matches at the position where the previous match ended. </p> <pre><code>String str = "00122334455667788990875645346787659870984780"; String[] arr = str.split("(?&lt;=\\G..)"); System.out.println(Arrays.asList(arr)); </code></pre> <p>prints:</p> <pre><code>[00, 12, 23, 34, 45, 56, 67, 78, 89, 90, 87, 56, 45, 34, 67, 87, 65, 98, 70, 98, 47, 80] </code></pre> <hr /> <p>Here's how split is done on your string:</p> <pre><code> " 00 1 2 2334455667788990875645346787659870984780" (whitespaces represent empty string) // | | | // split, no-split, split -&gt; gives 12 // | | | | // \ / \ / // gives 00 as the preceding two characters are `1` and `0`. // but 0 is already considered for the previous empty string </code></pre> <hr /> <p><strong>Reference:</strong></p> <ul> <li><a href="http://www.regular-expressions.info/continue.html" rel="nofollow">Continuing match using \G - Regular Expression info</a></li> </ul> <hr /> <p>If runtime performance is an issue, then you can go with simple looping:</p> <pre><code>String str = "00122334455667788990875645346787659870984780"; List&lt;String&gt; list = new ArrayList&lt;String&gt;(); for (int i = 0; i &lt; str.length(); i += 2) { list.add(str.substring(i, i + 2)); } System.out.println(list); </code></pre> <p>But you can check for yourself, whether the regex split is really a performance bottleneck for large string, and benchmark both of them appropriately.</p> <hr /> <p>I benchmarked both the methods - split, and loop. And as expected loop is almost 4-5 times more efficient than split for a string of length say <code>1000</code>.</p> <pre><code>public static void usingSplit(String str) { String[] arr = str.split("(?&lt;=\\G..)"); List&lt;String&gt; list = Arrays.asList(arr); } public static void usingLoop(String str) { List&lt;String&gt; list = new ArrayList&lt;String&gt;(); for (int i = 0; i &lt; str.length(); i += 2) { list.add(str.substring(i, i + 2)); } } // Warm up JVM for (int i = 0; i &lt; 1000000; ++i) { usingSplit(str); } for (int j = 0; j &lt; 1000000; j++) { usingLoop(str); } long nano = System.nanoTime(); for (int i = 0; i &lt; 1000000; ++i) { usingSplit(str); } System.out.println("Time with usingSplit(): " + (System.nanoTime() - nano) * 1.0 / Math.pow(10, 9) + " Seconds"); nano = System.nanoTime(); for (int j = 0; j &lt; 1000000; j++) { usingLoop(str); } System.out.println("Time with usingLoop(): " + (System.nanoTime() - nano) * 1.0 / Math.pow(10, 9) + " Seconds"); </code></pre> <p>Output on few successive runs:</p> <pre><code>Run 1: Time with usingSplit(): 34.391315143 Seconds Time with usingLoop(): 7.515221612 Seconds Run 2: Time with usingSplit(): 33.41518869 Seconds Time with usingLoop(): 7.868896218 Seconds </code></pre> <p>If someone thinks that the benchmark result is flawed, then please make a note of it in comments.</p>
 

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