How to Remove the String "\N" from Within a Ruby String

How can I remove the string \n from within a Ruby string?

You need to use "\n" not '\n' in your gsub. The different quote marks behave differently.

Double quotes " allow character expansion and expression interpolation ie. they let you use escaped control chars like \n to represent their true value, in this case, newline, and allow the use of #{expression} so you can weave variables and, well, pretty much any ruby expression you like into the text.

While on the other hand, single quotes ' treat the string literally, so there's no expansion, replacement, interpolation or what have you.

In this particular case, it's better to use either the .delete or .tr String method to delete the newlines.

See here for more info

Ruby: Remove new lines, carriage returns from text

The most efficient way to perform this operation is with String#delete (or #delete!):

text.delete!("\r\n\\")
p text
puts
puts text

Output:

"An Ox came down to a reedy pool to drink. As he splashed heavily into
the water, he crushed a young Frog into the mud.The old Frog soon
missed the little one and asked his brothers and sisters what had
become of him.\"A great big monster,\" said one of them, \"stepped on
little brother with one of his huge feet!\"\"Big, was he!\" said the
old Frog, puffing herself up. \"Was he as big as this?\"\"Oh, much
bigger!\" they cried.The Frog puffed up still more.\"He could not have
been bigger than this,\" she said. But the little Frogs all declared
that the monster was much, much bigger and the old Frog kept puffing
herself out more and more until, all at once, she burst."

An Ox came
down to a reedy pool to drink. As he splashed heavily into the water,
he crushed a young Frog into the mud.The old Frog soon missed the
little one and asked his brothers and sisters what had become of
him."A great big monster," said one of them, "stepped on little
brother with one of his huge feet!""Big, was he!" said the old Frog,
puffing herself up. "Was he as big as this?""Oh, much bigger!" they
cried.The Frog puffed up still more."He could not have been bigger
than this," she said. But the little Frogs all declared that the
monster was much, much bigger and the old Frog kept puffing herself
out more and more until, all at once, she burst.

Benchmark results:

Warming up --------------------------------------
String#gsub 2.826k i/100ms
String#tr 35.794k i/100ms
String#delete 37.147k i/100ms
Calculating -------------------------------------
String#gsub 29.801k (± 2.8%) i/s - 149.778k in 5.030044s
String#tr 399.391k (± 3.3%) i/s - 2.004M in 5.024297s
String#delete 411.065k (± 4.0%) i/s - 2.080M in 5.068783s

I used /\R+|\// for the String#gsub method.

ruby: How to remove the string “\n” right before a number from a string?

You may try replacing \n(?=\d) with empty string:

input = "AUTO TEST\nJANE DOE-RIGHT\n12097105\nJOE BIN\n1216515\nREGRESSION SUE TEST\n10023436"
output = input.gsub(/\n(?=\d)/, '')
puts output

This prints:

AUTO TEST
JANE DOE-RIGHT12097105
JOE BIN1216515
REGRESSION SUE TEST10023436

How do I remove carriage returns with Ruby?

What do you get when you do puts lines? That will give you a clue.

By default File.open opens the file in text mode, so your \r\n characters will be automatically converted to \n. Maybe that's the reason lines are always equal to lines2. To prevent Ruby from parsing the line ends use the rb mode:

C:\> copy con lala.txt
a
file
with
many
lines
^Z

C:\> irb
irb(main):001:0> text = File.open('lala.txt').read
=> "a\nfile\nwith\nmany\nlines\n"
irb(main):002:0> bin = File.open('lala.txt', 'rb').read
=> "a\r\nfile\r\nwith\r\nmany\r\nlines\r\n"
irb(main):003:0>

But from your question and code I see you simply need to open the file with the default modifier. You don't need any conversion and may use the shorter File.read.

How to use Ruby's gsub function to replace excessive '\n' on a string

I think you're looking for:

string.gsub( / *\n+/, "\n" )

This searches for zero or more spaces followed by one or more newlines, and replaces the match with a single newline.

Removing line breaks in Ruby

Single-quoted strings do not process most escape sequences. So, when you have this

'\n'

it literally means "two character string, where first character is backslash and second character is lower-case 'n'". It does not mean "newline character". In order for \n to mean newline char, you have to put it inside of double-quoted string (which does process this escape sequence). Here are a few examples:

"Remove \n".delete('\n') # => "Remove \n" # doesn't match
'Remove \n'.delete('\n') # => "Remove \\" # see below

'Remove \n'.delete("\n") # => "Remove \\n" # no newline in source string
"Remove \n".delete("\n") # => "Remove " # properly removed

NOTE that backslash character in this particular example (second line, using single-quoted string in delete call) is simply ignored, because of special logic in the delete method. See doc on String#count for more info. To bypass this, use gsub, for example

'Remove \n'.gsub('\n', '') # => "Remove "

what is the best way to remove the last n characters of a string (in Ruby)?

You can use ranges.

"string"[0..-4]

Deleting all special characters from a string - ruby

You can do this

a.gsub!(/[^0-9A-Za-z]/, '')

Remove all a tags from string

You can use strip_tags (to strip all tags) or strip_links (to strip just links).

In Rails console:

> text = '<div>blah blah blah.<br /><br /></div>\r\n<div><a href=\"http://www.google.com\">Google</a><br />Another link: <br /> <a href=\"http://www.test.com\">Test Link</a><br /><br /></div>'
=> "<div>blah blah blah.<br /><br /></div>\\r\\n<div><a href=\\\"http://www.google.com\\\">Google</a><br />Another link: <br /> <a href=\\\"http://www.test.com\\\">Test Link</a><br /><br /></div>"
> helper.strip_tags(text)
=> "blah blah blah.\\r\\nGoogleAnother link: Test Link"


Related Topics



Leave a reply



Submit