在字符串后面添加空格

时间:2021-12-28 16:54:21

I have a string:

我有一个字符串:

str="Myname"

I want to add four white spaces after the string. What I did was:

我想在字符串后面加4个空格。我所做的是:

str=str+"    "+"somename"

When I print the str as <%= str %>, the output shows only one white space. How can I make this work? I also tried:

当我将str打印为<%= str %>时,输出只显示一个空格。我怎样才能让它工作呢?我也试过:

str=str+" "*4+"somename"  

This also gives the same output as the one above gives. I don't want to print this. The string is used as a Ruby variable for more other operations. I can make it in Ruby, but not in RoR.

这也提供了与上面给出的输出相同的输出。我不想打印出来。该字符串用作其他操作的Ruby变量。我可以用Ruby,但不能用RoR。

6 个解决方案

#1


5  

This has to do with how HTML handles whitespace. Which I assume you are using based on the Erb like syntax you used. If you really must output whitespace use &nbsp;.

这与HTML如何处理空格有关。我猜您使用的是基于您使用的类似Erb的语法。如果您确实需要输出空格,请使用& !

But I suggest you try to fix this with CSS.

但是我建议您尝试用CSS来解决这个问题。

#2


2  

Following your same structure, it would be

按照你的结构,它会是

str = raw(str + "&nbsp;&nbsp;&nbsp;&nbsp;" + "somename")

原始的+“somename”)

In HTML, only a single white space in counted, regardless of how many. So you must use the HTML Entity &nbsp; (means Non Breaking SPace) to show more than one space.

在HTML中,无论有多少个空格,都只有一个空格。所以你必须使用HTML实体(意思是不间断的空间)显示一个以上的空间。

The raw part is required because Rails, by default, does not allow HTML Entities or any actual HTML to be output by strings, because they can be used by nefarious users to attack your site and/or users.

原始部分是必需的,因为Rails默认不允许通过字符串输出HTML实体或任何实际的HTML,因为恶意用户可以使用它们攻击站点和/或用户。

Therefore, you must use raw and also use &nbsp; in Rails.

因此,你必须使用生料,也要使用。在Rails。

#3


0  

There may be 4 spaces in your variable but browser truncate the extra spaces after 1 space so you may not be viewing them. try following

您的变量中可能有4个空格,但浏览器会截断1个空格后的额外空间,因此您可能不会看到它们。尝试后

<pre>
<%= str %>
</pre>

You will see spaces are added to your code. To achieve what you are trying to do put   rather then blank space

您将看到空格被添加到代码中。为了实现你想要做的,你应该把它放在空白的地方

#4


0  

if you try following code in console you will see that the issue is with your html;

如果您尝试在控制台中遵循代码,您将看到问题出在您的html;

str="Myname"
str=str+"    "+"somename"

=> "Myname    somename"

try it as follow

尝试它,遵循

<pre>
  <%= str %>
</pre>

#5


0  

you can replace the spaces with html encoding string and then render with html_safe or raw like,

可以用html编码字符串替换空格,然后用html_safe或raw格式呈现,

<%= raw "Myname    somename".gsub(/\s/, "&nbsp;") %>

#6


0  

<pre>I am a bad web programmer</pre>

pre tags of the bane of existence and should be avoided at all costs. Use this in your erb.

存在的*的预先标记,应该不惜一切代价避免。在你的erb中使用这个。

<%= raw(CGI.escapeHTML(str).gsub(/\s/, '&nbsp;')) %>

#1


5  

This has to do with how HTML handles whitespace. Which I assume you are using based on the Erb like syntax you used. If you really must output whitespace use &nbsp;.

这与HTML如何处理空格有关。我猜您使用的是基于您使用的类似Erb的语法。如果您确实需要输出空格,请使用& !

But I suggest you try to fix this with CSS.

但是我建议您尝试用CSS来解决这个问题。

#2


2  

Following your same structure, it would be

按照你的结构,它会是

str = raw(str + "&nbsp;&nbsp;&nbsp;&nbsp;" + "somename")

原始的+“somename”)

In HTML, only a single white space in counted, regardless of how many. So you must use the HTML Entity &nbsp; (means Non Breaking SPace) to show more than one space.

在HTML中,无论有多少个空格,都只有一个空格。所以你必须使用HTML实体(意思是不间断的空间)显示一个以上的空间。

The raw part is required because Rails, by default, does not allow HTML Entities or any actual HTML to be output by strings, because they can be used by nefarious users to attack your site and/or users.

原始部分是必需的,因为Rails默认不允许通过字符串输出HTML实体或任何实际的HTML,因为恶意用户可以使用它们攻击站点和/或用户。

Therefore, you must use raw and also use &nbsp; in Rails.

因此,你必须使用生料,也要使用。在Rails。

#3


0  

There may be 4 spaces in your variable but browser truncate the extra spaces after 1 space so you may not be viewing them. try following

您的变量中可能有4个空格,但浏览器会截断1个空格后的额外空间,因此您可能不会看到它们。尝试后

<pre>
<%= str %>
</pre>

You will see spaces are added to your code. To achieve what you are trying to do put   rather then blank space

您将看到空格被添加到代码中。为了实现你想要做的,你应该把它放在空白的地方

#4


0  

if you try following code in console you will see that the issue is with your html;

如果您尝试在控制台中遵循代码,您将看到问题出在您的html;

str="Myname"
str=str+"    "+"somename"

=> "Myname    somename"

try it as follow

尝试它,遵循

<pre>
  <%= str %>
</pre>

#5


0  

you can replace the spaces with html encoding string and then render with html_safe or raw like,

可以用html编码字符串替换空格,然后用html_safe或raw格式呈现,

<%= raw "Myname    somename".gsub(/\s/, "&nbsp;") %>

#6


0  

<pre>I am a bad web programmer</pre>

pre tags of the bane of existence and should be avoided at all costs. Use this in your erb.

存在的*的预先标记,应该不惜一切代价避免。在你的erb中使用这个。

<%= raw(CGI.escapeHTML(str).gsub(/\s/, '&nbsp;')) %>