java - Replace newline and extra spaces -


i have text this:

[spaces] [spaces]  line [spaces] [newline]<br/>   has been [spaces] broken [newline]<br/>  [newline]<br/>  [newline]<br/>  multiple part[spaces] [newline]<br/>  

i want to:

  • remove single newline space
  • remove multiple newline single newline
  • remove multiple-white spaces single white space
  • keep single white spaces are.

so text above should this:

this line has been broken<br/> multiple part. 

i using java (for android app). have tried few solutions here on trim() seems work strips spaces @ beginning , end of text. other replaceall() ways don't work in case.

any appreciated. thank you.

update used code: replaceall("\n", " ").replaceall("[\n]{2,}", "\n").replaceall(" +", " ")

but not working expected.

it seems need these 3 replacements simultaneously instead of passing string 1 replaceall()

it should see if there 2 of more \n replace 1 \n , if there 1 \n replace space.

i think should first replace 2 or more \n word twofeeds , replace single \n space. after again replace twofeeds 1 \n

update 2 works expected. not sure how slow when pass 7200 text records db table.

replaceall("[\n]{2,}", "twofeeds").replaceall("\n", " ").replaceall(" +", " ").replaceall("<br/>", "\n").replaceall("twofeeds", "\n")

if there anyway improve please let me know.

using regex can remove spaces , lines.

code:

string text = text.replaceall("\\s+", " "); 

this give output have expected.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -