在Rails中截断字符串:“...”显示字符串的长度

时间:2023-02-05 21:36:28

I'm currently trying to truncate any strings greater than 65 characters.

我正在尝试截断任何超过65个字符的字符串。

My code is

我的代码是

<title><%= truncate(title.html_safe, length:65) %></title>

It works great for titles longer than 65 characters. But titles that are exactly 65 character still get truncated.

它适用于超过65个字符的标题。但是正好65个字符的标题仍然会被截断。

For example:

例如:

title:"This post is exactly 65 characters characters characters characte"

标题:“这篇文章正好是65个字符的字符字符”

shows on page as "This post is exactly 65 characters characters characters chara..."

在页面上显示为“此帖子正好是65个字符的字符字符...”

Should I not be using truncate?

我不应该使用截断吗?

5 个解决方案

#1


5  

truncate is the right method. This might be a bug in your version of rails? Here's what I get on my console:

truncate是正确的方法。这可能是您的rails版本中的错误?这是我在控制台上得到的内容:

[5] pry(main)> helper.truncate("This post is exactly 56 characters characters characters characte", length: 65)
=> "This post is exactly 56 characters characters characters characte"
[6] pry(main)> helper.truncate("This post is exactly 56 characters characters characters characte", length: 64)
=> "This post is exactly 56 characters characters characters char..."

I'm running Rails 4.0.4 in this example.

我在这个例子中运行Rails 4.0.4。

#2


3  

If you are using rails 4.2 or above then you can use truncate_words method.

如果您使用的是rails 4.2或更高版本,则可以使用truncate_words方法。

<title><%= (title.html_safe).truncate_words(5) %></title>

#3


1  

the truncate method work just as expected. Please output the length after your invocation of title.html_safe, is it possible that your string contains some trailing spaces?

truncate方法正如预期的那样工作。请在调用title.html_safe后输出长度,您的字符串是否可能包含一些尾随空格?

#4


1  

title = "This post is exactly 56 characters characters characters characte".length => 65

title =“这篇文章正好是56个字符的字符字符”.length => 65

<title><%= truncate(title.html_safe, length:56) %></title> # will be truncated

<%= truncate(title.html_safe,length:56)%> </ title>#将被截断</p>

<title><%= truncate(title.html_safe, length:65) %></title> # this should work fine, I just tried.

<%= truncate(title.html_safe,length:65)%> </ title>#这应该可以正常工作,我试过了。</p>

Rails 3.2.17

Rails 3.2.17

#5


1  

We call it Ellipsis. Just to add to the above answers you can achieve ellipsis by css, jquery and rails.

我们称之为省略号。只需添加上述答案,您就可以通过css,jquery和rails实现省略号。

css:

CSS:

.truncate {
  width: 250px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

Jquery:

jQuery的:

There is a really good jquery plugin dotdotdot. I have used it one of my projects and it works like a charm. It uses height and width of your parent element smartly to show ellipsis.

有一个非常好的jquery插件dotdotdot。我使用过我的一个项目,它就像一个魅力。它巧妙地使用父元素的高度和宽度来显示省略号。

Rails:

扶手:

Rails has a text helper truncate which can be used like:

Rails有一个文本助手截断,可以像下面这样使用:

<%= truncate("Once upon a time in a world far far away", length: 65) %>

CSS is obviously the quickest way to achieve ellipsis but it has its disadvantages that it doesn't provide a way to limit your characters and it's tricky to get it to work for multiple lines.

CSS显然是实现省略号的最快方法,但它的缺点是它不提供限制字符的方法,并且让它适用于多行是很棘手的。

Jquery plugin dotdotdot works great for multiple lines but i don't think it has option to specify character limit either.

Jquery插件dotdotdot适用于多行,但我认为它没有选项来指定字符限制。

Rails truncate it obviously server side and will allow you an option to specify character limit.

Rails明显将服务器端截断,并允许您选择指定字符限制。

#1


5  

truncate is the right method. This might be a bug in your version of rails? Here's what I get on my console:

truncate是正确的方法。这可能是您的rails版本中的错误?这是我在控制台上得到的内容:

[5] pry(main)> helper.truncate("This post is exactly 56 characters characters characters characte", length: 65)
=> "This post is exactly 56 characters characters characters characte"
[6] pry(main)> helper.truncate("This post is exactly 56 characters characters characters characte", length: 64)
=> "This post is exactly 56 characters characters characters char..."

I'm running Rails 4.0.4 in this example.

我在这个例子中运行Rails 4.0.4。

#2


3  

If you are using rails 4.2 or above then you can use truncate_words method.

如果您使用的是rails 4.2或更高版本,则可以使用truncate_words方法。

<title><%= (title.html_safe).truncate_words(5) %></title>

#3


1  

the truncate method work just as expected. Please output the length after your invocation of title.html_safe, is it possible that your string contains some trailing spaces?

truncate方法正如预期的那样工作。请在调用title.html_safe后输出长度,您的字符串是否可能包含一些尾随空格?

#4


1  

title = "This post is exactly 56 characters characters characters characte".length => 65

title =“这篇文章正好是56个字符的字符字符”.length => 65

<title><%= truncate(title.html_safe, length:56) %></title> # will be truncated

<%= truncate(title.html_safe,length:56)%> </ title>#将被截断</p>

<title><%= truncate(title.html_safe, length:65) %></title> # this should work fine, I just tried.

<%= truncate(title.html_safe,length:65)%> </ title>#这应该可以正常工作,我试过了。</p>

Rails 3.2.17

Rails 3.2.17

#5


1  

We call it Ellipsis. Just to add to the above answers you can achieve ellipsis by css, jquery and rails.

我们称之为省略号。只需添加上述答案,您就可以通过css,jquery和rails实现省略号。

css:

CSS:

.truncate {
  width: 250px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

Jquery:

jQuery的:

There is a really good jquery plugin dotdotdot. I have used it one of my projects and it works like a charm. It uses height and width of your parent element smartly to show ellipsis.

有一个非常好的jquery插件dotdotdot。我使用过我的一个项目,它就像一个魅力。它巧妙地使用父元素的高度和宽度来显示省略号。

Rails:

扶手:

Rails has a text helper truncate which can be used like:

Rails有一个文本助手截断,可以像下面这样使用:

<%= truncate("Once upon a time in a world far far away", length: 65) %>

CSS is obviously the quickest way to achieve ellipsis but it has its disadvantages that it doesn't provide a way to limit your characters and it's tricky to get it to work for multiple lines.

CSS显然是实现省略号的最快方法,但它的缺点是它不提供限制字符的方法,并且让它适用于多行是很棘手的。

Jquery plugin dotdotdot works great for multiple lines but i don't think it has option to specify character limit either.

Jquery插件dotdotdot适用于多行,但我认为它没有选项来指定字符限制。

Rails truncate it obviously server side and will allow you an option to specify character limit.

Rails明显将服务器端截断,并允许您选择指定字符限制。