简体   繁体   中英

Removing “\n” from string

When I save the output of a bash command (executed in backticks) to an instance variable, the browser, or more specifically, the variable, removes the "\\n" characters. I checked the source code of the page, and it displays like this:

line 1
line 2

line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10

But the browser displays it like this:

line 1 line 2 line 3 line 4 line 5 line 6 line 7 line 8 line 9 line 10

I put this on a method that displays the output:

@variable.each_line do |line|
    if line !~ /word1| word2/
        line.sub("Word_I_don't_want", '')
    end
end

But the variable won't be compared to the regular expression, and is printed as a whole. The browser displays a string without <br> as a single line, but the source code shows that new lines are being identified, but not processed. I tried

line.sub("\n", "<br>")

but it didn't work either. Hope someone has some tips for me.

SOLVED:

Thanks to ilan berci I could print the desired values, although the Tin Man pointed out that text should be processed on the controller. So I modified my method:

tempString = String.new

@redes.each_line do |line|
    if line !~ /word1|word2/
        tempString = tempString + line.sub("word_I_don't_want", "").sub("\n", "<br>")
    end
end

@redes = tempString

Because processing the text in the .erb was filling the page code with <br> for every unwanted line (the <br> was casted in every .each_line iteration).

Thanks for the help!


  <% @variable.each_line do |line| %>
    <% line = if line !~ /word1| word2/ %>
      <%line.sub("Word_I_don't_want", '') %>
    <% end %>
    <%= line %>
    <br/>
  <% end %>

The problem is how browsers display line-ends and white-space. They "gobble" spaces, tabs, vertical-tabs and line-endings, squeezing them into a single space when displayed.

For instance:

<html>
<body>
foo
bar
</body>
</html>

Will display as foo bar in a browser, even though there is line-end ("\\n") between foo and bar .

You can fix that a couple ways:

<html>
<body>
foo<br>
bar<br>
</body>
</html>

Or:

<html>
<body>
<pre>
foo
bar
</pre>
</body>
</html>

would display as:

foo
bar

Whereas:

<html>
<body>
<p>foo</p>
<p>bar</p>
</body>
</html>

would display as:

foo

bar

Browser barely honor whitespace. Words with multiple spaces and line-ends will display as if they only have a single space. If I had a string of characters:

foo\t \n bar

in a page, they would display as:

foo bar

because the browser is trying to be helpful.

How do you "fix" your code? You either wrap your text in a <pre> block, append <br> to the lines where you want line-breaks, or do something fun with CSS to adjust the defaults of <p> or <div> tags. There are lots of ways to get there, you just need to know what you're working with and pick a path.

I've used this in my applications:

def replace_newlines_with_br_tags(text)
  h(text).gsub(/\r\n/, '<br />').gsub(/\r/, '<br />').gsub(/\n/, '<br />')
end

You can add this to your application helper and use it in your views, etc.

@variable.gsub(/(?:\n\r?|\r\n?)/, '<br>')

这里有学分

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM