Ruby 如何在最近的空格处截断句子?

Ruby 如何在最近的空格处截断句子?,ruby,Ruby,我们有一个句子和一个字符限制。我们想截断句子,如果它超过了字符限制,但只在一个空间,而不是在一个词的中间。 这就是我们目前的情况: def shortened_headline(max_length) return @headline unless @headline.length > max_length @headline[0..max_length] end 修剪完标题后,您可以使用rindex从数组或字符串的右侧查找某些内容的索引 比如: sub_leng

我们有一个句子和一个字符限制。我们想截断句子,如果它超过了字符限制,但只在一个空间,而不是在一个词的中间。

这就是我们目前的情况:

  def shortened_headline(max_length)
    return @headline unless @headline.length > max_length
    @headline[0..max_length]
  end

修剪完标题后,您可以使用
rindex
从数组或字符串的右侧查找某些内容的索引

比如:

sub_length=@headline[0..max_length].rindex(' ')
将为您提供标题中最后一个空格的位置。如果您想在字符串中找到最后一个非字母数字字符,那么也可以将其与正则表达式一起使用,这样就可以打断最后一个空格或标点符号


修剪完标题后,您可以使用
rindex
从数组或字符串的右侧查找某物的索引

比如:

sub_length=@headline[0..max_length].rindex(' ')
将为您提供标题中最后一个空格的位置。如果您想在字符串中找到最后一个非字母数字字符,那么也可以将其与正则表达式一起使用,这样就可以打断最后一个空格或标点符号

您应该使用。它查找字符串第一次出现的索引,并接受和偏移

注意:这个实现在最大长度之后的第一个空格中剪切字符串(我刚刚意识到,这可能不是您想要的)。如果您需要在最大长度之前的第一个空格进行切割,请参见@glenatron的答案

def shortened_headline(headline, max_length)
  return headline if headline.length < max_length

  space_pos = headline.index(' ', max_length)
  headline[0..space_pos-1]
end

h = 'How do you truncate a sentence at the nearest space?'

h[0..4] # => "How d"
shortened_headline(h, 5) # => "How do"

h[0..10] # => "How do you "
shortened_headline(h, 10) # => "How do you"

h[0..15] # => "How do you trunc"
shortened_headline(h, 15) # => "How do you truncate"
def缩短标题(标题,最大长度)
如果headline.length“如何d”
缩短标题(h,5)#=>“你好”
h[0..10]#=>“你好”
缩短标题(h,10)#=>“你好”
h[0..15]#=>“你是怎么做到的?”
缩短标题(h,15)#=>“如何截断”
您应该使用。它查找字符串第一次出现的索引,并接受和偏移

注意:这个实现在最大长度之后的第一个空格中剪切字符串(我刚刚意识到,这可能不是您想要的)。如果您需要在最大长度之前的第一个空格进行切割,请参见@glenatron的答案

def shortened_headline(headline, max_length)
  return headline if headline.length < max_length

  space_pos = headline.index(' ', max_length)
  headline[0..space_pos-1]
end

h = 'How do you truncate a sentence at the nearest space?'

h[0..4] # => "How d"
shortened_headline(h, 5) # => "How do"

h[0..10] # => "How do you "
shortened_headline(h, 10) # => "How do you"

h[0..15] # => "How do you trunc"
shortened_headline(h, 15) # => "How do you truncate"
def缩短标题(标题,最大长度)
如果headline.length“如何d”
缩短标题(h,5)#=>“你好”
h[0..10]#=>“你好”
缩短标题(h,10)#=>“你好”
h[0..15]#=>“你是怎么做到的?”
缩短标题(h,15)#=>“如何截断”

Rails使用各种方便的方法扩展了
String
类,其中有一个
truncate
方法,您可以传递一个
:separator
选项。即使不使用Rails,也可以简单地复制它们的实现。请参阅位于的文档


(您可以单击“show source”查看实际实现)

Rails使用各种方便的方法扩展了
String
类,其中包括
truncate
方法,您可以传递
:separator
选项。即使不使用Rails,也可以简单地复制它们的实现。请参阅位于的文档


(您可以单击“显示源代码”查看实际实现)

查看ActiveSupport的字符串核心扩展,特别是方法

从文档中:

The method truncate returns a copy of its receiver truncated after a given length:

    "Oh dear! Oh dear! I shall be late!".truncate(20)
    # => "Oh dear! Oh dear!..."
按如下方式访问它:

irb(main):001:0> require 'active_support/core_ext/string/filters'
irb(main):002:0> 'how now brown cow'.truncate(10)
=> "how now..."
truncate
方法可以在不需要额外装饰的情况下关闭省略号


ActiveSupport在不久前进行了重构,允许我们挑选所需的功能,而无需提取完整的库。它充满了美好。有更多的信息。

看看ActiveSupport的字符串核心扩展,特别是方法

从文档中:

The method truncate returns a copy of its receiver truncated after a given length:

    "Oh dear! Oh dear! I shall be late!".truncate(20)
    # => "Oh dear! Oh dear!..."
按如下方式访问它:

irb(main):001:0> require 'active_support/core_ext/string/filters'
irb(main):002:0> 'how now brown cow'.truncate(10)
=> "how now..."
truncate
方法可以在不需要额外装饰的情况下关闭省略号


ActiveSupport在不久前进行了重构,允许我们挑选所需的功能,而无需提取完整的库。它充满了美好。有更多信息。

这就是Rails如此出色的原因。这就好像他们真的想到了一切。这就是Rails如此棒的原因。好像他们真的想到了一切。