Replace newline and extra spaces

Related searches

I have text that is something like this:

[spaces] This is a [spaces]  line [spaces] [newline]<br/>  
which has been [spaces] broken [newline]<br/> 
[newline]<br/> 
[newline]<br/> 
into multiple part[spaces] [newline]<br/> 

I want to:

  • remove single newline with a space
  • remove multiple newline with single newline
  • remove multiple-white spaces with single white space
  • keep single white spaces as they are.

So text above should be like this:

This is a line which has been broken<br/>
into multiple part.

I am using java (for android app). I have tried a few solutions here on SO but only trim() seems to work which strips spaces at beginning and the end of text. Other ReplaceAll() ways don't work in my case.

Any help will be appreciated. Thank you.

UPDATE I used this code: replaceAll("\n", " ").replaceAll("[\n]{2,}", "\n").replaceAll(" +", " ")

But it is not working as expected.

It seems I need something that would do all these three replacements simultaneously instead of passing string from one to another replaceAll()

It should see if there are two of more \n then replace it with one \n AND if there was already only one \n then replace that with a space.

I think I should first replace two or more \n with some word like TWOFEEDS and then replace single \n with space. After that I again replace TWOFEEDS with just one \n

UPDATE 2 This works as expected. But I am not sure how slow it will be when I pass it 7200 text records from DB table.

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

If there is anyway to improve please let me know.

Using Regex you can remove extra spaces and lines.

Code:

String text = text.replaceAll("\\s+", " ");

This will give you the output you have expected.

Remove newline, space and tab characters from a string in Java, To remove newline, space and tab characters from a string, replace them with empty as shown below.replaceAll([\\n\\t ], );Above, the new line,� World's simplest browser-based utility for converting newlines to spaces in text. Load your text in the input form on the left and you'll instantly get text with spaces instead of newlines in the output area. Powerful, free, and fast. Load text – get spaces instead of newlines. Created by developers from team Browserling.

If anyone needs answer to this kind of question then I have finally settled with this

replaceAll("\n", " ").replaceAll("[\n]{2,}", "\n").replaceAll(" +", " ")

This is working good even for 7k records. Also, you may want to use trim() before applying above replaceAll() use it like yourtext.trim().replaceAll....(basically above code)

Remove Line Breaks with Javascript, To remove all extra white spaces just use this javascript code after the line break removal code: //Replace all double white spaces with single spaces someText� To remove newline, space and tab characters from a string, replace them with empty as shown below. replaceAll (" [\ \\t ]", ""); Above, the new line, tab, and space will get replaced with empty, since we have used replaceAll () The following is the complete example.

Try this, to replace multiple new lines with single new line:

yourStr.replaceAll("[\r\n]+", "\r\n")

Removing Whitespace (and Line Breaks) - Power Tips, A lesser known fact is that Trim() will also eat away leading and trailing line breaks: This example trims away whitespace, dots, dashes, and line breaks:. Now there may be places in the text where double line spaces appear when we just want a single space. To remove all extra white spaces just use this javascript code after the line break removal code: //Replace all double white spaces with single spaces someText = someText.replace(/\s+/g," "); This javascript regex finds multiple whitespaces and replaces them with a single white space.

how would I trim a mix of newline and space characters from the , @srinivasm: i've noticed also that trim() doesn't handle extra newlines. iterate over the string to substitute both with a space and then tidy it all up with trim():. To remove extra spaces between paragraphs, follow the steps below. Open Microsoft Word and the document you want to edit. On the Home tab, click Replace or press the keyboard shortcut Ctrl+H. In the Find and Replace window, in the Find what section, type ^p^p and in the Replace with section, type ^p (only once).

How to replace both space and newline form the string using regex , If you just want to get rid of all the white space, you can just delete it: lstIQCodes = IQcodeString.deleteWhitespace().split(',');. ReplaceAll� World's simplest browser-based utility for converting spaces to newlines in text. Load your text in the input form on the left and you'll instantly get text with newlines instead of spaces in the output area. Powerful, free, and fast. Load text – get newlines instead of spaces.

Select the cells that you want to search On the keyboard, press Ctrl + H to open the Find and Replace dialog box, with the Replace tab active Click in the Find What box On the keyboard, press Ctrl + J to enter the line break character -- NOTE: No text will appear in the Find What box -- just a small blinking dot

Comments
  • Welcome to Stack Overflow! Please take the tour to see how the site works and what questions are on topic here, and edit your question accordingly. See also: Why is "Can someone help me?" not an actual question?
  • regex was born to solve this problem. Learn about regular expressions.
  • stackoverflow.com/a/31032620/5281666
  • Thank you for the response. It removes multiple newlines with spaces too. I would like to replace multiple newlines with single newline. Thank you.
  • Can't understand your question. what is your expected output?
  • My bad. I updated the expected output. Two or more \n' should be replaced with only one. One \n should be replaced with one space. And two or more spaces should be replaced with one space.
  • @BhuvaneshBS - could you please explain the regex ?
  • Why using 3 replaceAll when a single one is enough?