How to Remove Line Breaks (No Characters!) from the String

How to remove line breaks (no characters!) from the string?

You should be able to replace it with a preg that removes all newlines and carriage returns. The code is:

preg_replace( "/\r|\n/", "", $yourString );

Even though the \n characters are not appearing, if you are getting carriage returns there is an invisible character there. The preg replace should grab and fix those.

remove all line breaks (enter symbols) from the string using R

You need to strip \r and \n to remove carriage returns and new lines.

x <- "foo\nbar\rbaz\r\nquux"
gsub("[\r\n]", "", x)
## [1] "foobarbazquux"

Or

library(stringr)
str_replace_all(x, "[\r\n]" , "")
## [1] "foobarbazquux"

Remove all line breaks from a long string of text

How do you enter line breaks with raw_input? But, once you have a string with some characters in it you want to get rid of, just replace them.

>>> mystr = raw_input('please enter string: ')
please enter string: hello world, how do i enter line breaks?
>>> # pressing enter didn't work...
...
>>> mystr
'hello world, how do i enter line breaks?'
>>> mystr.replace(' ', '')
'helloworld,howdoienterlinebreaks?'
>>>

In the example above, I replaced all spaces. The string '\n' represents newlines. And \r represents carriage returns (if you're on windows, you might be getting these and a second replace will handle them for you!).

basically:

# you probably want to use a space ' ' to replace `\n`
mystring = mystring.replace('\n', ' ').replace('\r', '')

Note also, that it is a bad idea to call your variable string, as this shadows the module string. Another name I'd avoid but would love to use sometimes: file. For the same reason.

How to remove line breaks from a file in Java?

You need to set text to the results of text.replace():

String text = readFileAsString("textfile.txt");
text = text.replace("\n", "").replace("\r", "");

This is necessary because Strings are immutable -- calling replace doesn't change the original String, it returns a new one that's been changed. If you don't assign the result to text, then that new String is lost and garbage collected.

As for getting the newline String for any environment -- that is available by calling System.getProperty("line.separator").

How to remove all line breaks from a string

Line breaks (better: newlines) can be one of Carriage Return (CR, \r, on older Macs), Line Feed (LF, \n, on Unices incl. Linux) or CR followed by LF (\r\n, on WinDOS). (Contrary to another answer, this has nothing to do with character encoding.)

Therefore, the most efficient RegExp literal to match all variants is

/\r?\n|\r/

If you want to match all newlines in a string, use a global match,

/\r?\n|\r/g

respectively. Then proceed with the replace method as suggested in several other answers. (Probably you do not want to remove the newlines, but replace them with other whitespace, for example the space character, so that words remain intact.)

Remove last comma from string with line breaks

This is what I ended up doing: I just added $Medio = rtrim($Medio,','.PHP_EOL) to remove last line break before removing the last comma as Alex Howansky pointed out.

$Medio=''; //variable to handle the result

$MedioPago='12345'; //variable with numbers given

$chars = str_split($MedioPago); //split the numbers


foreach($chars as $char)
{
if($char=='1')
{
$Medio.='"codigo": "01",'.PHP_EOL;
}
else if($char=='2')
{
$Medio.='"codigo": "02",'.PHP_EOL;
}
else if($char=='3')
{
$Medio.='"codigo": "03",'.PHP_EOL;
}
else if($char=='4')
{
$Medio.='"codigo": "04",'.PHP_EOL;
}
else if($char=='5')
{
$Medio.='"codigo": "05",'.PHP_EOL;
}
}

$Medio = rtrim($Medio,','.PHP_EOL);

Remove line breaks \n in R

You could specify na.strings and stringsAsFactors=FALSE in the read.csv/read.table. (I changed the delimiter to , and saved the input data)

 stk <- read.csv('Akash.csv', header=TRUE, stringsAsFactors=FALSE,
sep=",", na.strings="\\N")
head(stk,3)
# sku Stockout_start Stockout_End create_date
#1 0BX-164463 <NA> 1/29/2015 11:35 1/29/2015 11:35
#2 0BX-164463 2/11/2015 18:01 <NA> 2/11/2015 18:01
#3 0BX-164464 <NA> 1/29/2015 11:38 1/29/2015 11:38

If you need to replace multiple columns to "Date" class

 stk[-1] <- lapply(stk[-1], as.Date, format='%m/%d/%Y %H:%M') 
str(stk)
#'data.frame': 15 obs. of 4 variables:
#$ sku : chr " 0BX-164463" " 0BX-164463" " 0BX-164464" " 0BX-164464" ...
#$ Stockout_start: Date, format: NA "2015-02-11" ...
#$ Stockout_End : Date, format: "2015-01-29" NA ...
#$ create_date : Date, format: "2015-01-29" "2015-02-11" ...

Regex: Remove line breaks in double quote field?

It's not possible to capture multiple newlines in a single query if you're using a repeated capture group; the regex engine can only grab the last match. That being said, if you're using powergrep (or some other search-and-replace that can selectively replace capture groups, not the whole match), do you really need a one-liner?

^"(?:[^"\n]|(\n+))*", will look between " and ", and match any amount of text that doesn't include a ", keeping it within your quoted statement - or, alternatively, will capture the last group of newlines it finds. If your tool can remove/replace text only in the capture group, why not use this regex a few times in a row? It'll leave your typo-free rows alone, but will remove one chunk of whitespace from your erroneous rows each time it's run. (Try it here! Note that this matches all lines, but only captures whitespace for the badly formatted ones)

How will you know when you're done? Try using ^(?=.*\n.*)"[^"]*", - it'll match any lines in your csv file that still have newlines, but will ignore properly formatted lines. When this regex returns no matches, you can be confident your file is typo-free. (Try it here!)

It's not a very elegant solution, but if you run it enough times, you'll get rid of all the whitespace.

PHP Remove Unused Line Breaks Using nl2br

Old schooled but hope this works

$str = explode("\n", $str);

foreach($str as $val){
if(!empty($val)){
$result[] = $val;
}
}

$final = implode("\n", $result); //if you want line break use "<br>"
echo $final;


Related Topics



Leave a reply



Submit