39

テキストの長い文字列を特定の長さに切り捨てようとしていますが、切り捨てられた結果が空白で終わることも確認したいと思います。後で省略記号も追加します。

例:これ:

"This is a very long string that has more characters than I want in it."

これになります:

"This is a very long string that..."

私はこれから始めていますが、明らかにこれは空白で文字列を終了する問題を扱っていません。

<%= item.description[0..30] %>&hellip;
4

6 に答える 6

47

Rails 4+ を使用している場合は、組み込みのtruncateヘルパー メソッドを使用する必要があります。

<%= truncate item.description, length: 30, separator: /\w+/ %>

文字列「…」は、切り捨てられたテキストに追加されます。別の文字列を指定するには、:omissionオプションを使用しますomission: "xxx"

Rails 3.x の場合、:separatorオプションは文字列でなければなりません。多くの場合、与える:separator => " "ことは問題ありませんが、スペースのみをキャッチし、他の空白はキャッチしません。妥協点の 1 つはString#squish、 を使用することです。これは、空白のすべてのシーケンスを 1 つのスペースに置き換えます (また、先頭と末尾の空白も削除します)。たとえば、"foo\n\tbar ".squishyields"foo bar"です。次のようになります。

<%= truncate item.description.squish, :length => 30, :separator => /\w/,
                                      :omission => "&hellip;" %>
于 2012-02-29T17:21:41.747 に答える
44
s[0..30].gsub(/\s\w+\s*$/, '...')

30 文字の部分文字列が空白文字で終わっている場合、元の回答は機能しませんでした。これで解決です。

>> desc="This is some text it is really long"

>> desc[0..30].gsub(/\s\w+$/,'...')
"This is some text it is really "

>> desc[0..30].gsub(/\s\w+\s*$/,'...')
"This is some text it is..."
于 2012-02-29T17:21:32.900 に答える
7

@evfwcqcgの答えはとても良いです。私はそれがうまくいかないことに気づきました

  1. 文字列には、スペース以外の英数字以外の文字が含まれていました。
  2. 紐が希望の長さより短い。

デモンストレーション:

>> s = "How about we put some ruby method Class#Method in our string"
=> "How about we put some ruby method Class#Method in our string"
>> s[0..41].gsub(/\s\w+\s*$/, '...')
=> "How about we put some ruby method Class#Me"
>> s[0..999].gsub(/\s\w+\s*$/, '...')
=> "How about we put some ruby method Class#Method in our..."

これは私が期待したものではありません。

これを修正するために私が使用しているものは次のとおりです。

def truncate s, length = 30, ellipsis = '...'
  if s.length > length
    s.to_s[0..length].gsub(/[^\w]\w+\s*$/, ellipsis)
  else
    s
  end
end

テストを行うときの出力は次のとおりです。

>> s = "This is some text it is really long"
=> "This is some text it is really long"
>> truncate s
=> "This is some text it is..."

それでも期待どおりに動作します。

>> s = "How about we put some ruby method Class#Method in our string"
=> "How about we put some ruby method Class#Method in our string"
>> truncate s, 41
=> "How about we put some ruby method Class..."
>> truncate s, 999
=> "How about we put some ruby method Class#Method in our string"

これはもっと似ています。

于 2013-09-04T01:19:02.607 に答える
0
class String
  def trunca(length=100, ellipsis='...')
    self.length > length ? self[0..length].gsub(/\s*\S*\z/, '').rstrip+ellipsis : self.rstrip
  end
end

例:

-bash> irb
2.0.0p247 :001 > class String
2.0.0p247 :002?>     def trunca(length=100, ellipsis='...')
2.0.0p247 :003?>         self.length > length ? self[0..length].gsub(/\s*\S*\z/, '').rstrip+ellipsis : self.rstrip
2.0.0p247 :004?>       end
2.0.0p247 :005?>   end
 => nil 
2.0.0p247 :006 > s = "This is a very long string that has more characters than I want to display."
 => "This is a very long string that has more characters than I want to display." 
2.0.0p247 :007 > s.trunca(20)
 => "This is a very long..." 
2.0.0p247 :008 > s.trunca(31)
 => "This is a very long string that..." 
于 2014-04-18T17:15:49.333 に答える