title: Replace newline and extra spaces
body: <p>I have text that is something like this:</p>
  
  <pre><code>[spaces] This is a [spaces]  line [spaces] [newline]&lt;br/&gt;  
  which has been [spaces] broken [newline]&lt;br/&gt; 
  [newline]&lt;br/&gt; 
  [newline]&lt;br/&gt; 
  into multiple part[spaces] [newline]&lt;br/&gt; 
  </code></pre>
  
  <p>I want to:</p>
  
  <ul>
  <li>remove single newline with a space </li>
  <li>remove multiple newline with single newline</li>
  <li>remove multiple-white spaces with single white space</li>
  <li>keep single white spaces as they are.</li>
  </ul>
  
  <p>So text above should be like this:</p>
  
  <pre><code>This is a line which has been broken&lt;br/&gt;
  into multiple part.
  </code></pre>
  
  <p>I am using java (for android app). I have tried a few solutions here on SO but only <code>trim()</code> seems to work which strips spaces at beginning and the end of text. Other <code>ReplaceAll()</code> ways don't work in my case. </p>
  
  <p>Any help will be appreciated. 
  Thank you.</p>
  
  <p><strong>UPDATE</strong>
  I used this code:
  <code>replaceAll("\n", " ").replaceAll("[\n]{2,}", "\n").replaceAll(" +", " ")</code></p>
  
  <p>But it is not working as expected. </p>
  
  <p>It seems I need something that would do all these three replacements simultaneously instead of passing string from one to another <code>replaceAll()</code></p>
  
  <p>It should see if there are two of more <code>\n</code> then replace it with one <code>\n</code> AND if there was already only one <code>\n</code> then replace that with a space. </p>
  
  <p>I think I should first replace two or more <code>\n</code> with some word like TWOFEEDS and then replace single <code>\n</code> with space. After that I again replace TWOFEEDS  with just one <code>\n</code></p>
  
  <p><strong>UPDATE 2</strong>
  This works as expected. But I am not sure how slow it will be when I pass it 7200 text records from DB table.</p>
  
  <p><code>replaceAll("[\n]{2,}", "TWOFEEDS").replaceAll("\n", " ").replaceAll(" +", " ").replaceAll("&lt;br/&gt;", "\n").replaceAll("TWOFEEDS", "\n")</code></p>
  
  <p>If there is anyway to improve please let me know.</p>