2010-04-15 10 views

Antwort

14

können Sie interessiert sein truncate Funktion in TextHelper suchen:

truncate("Once upon a time in a world far far away") 
    # => Once upon a time in a world f... 

    truncate("Once upon a time in a world far far away", :length => 14) 
    # => Once upon a... 

    truncate("And they found that many people were sleeping better.", :length => 25, "(clipped)") 
    # => And they found that many (clipped) 

    truncate("And they found that many people were sleeping better.", :omission => "... (continued)", :length => 15) 
    # => And they found... (continued) 
+2

in Ruby können Sie auch nur einen Bereich geben, in einen String. mystring [0..119] – Beanish

4
'Once upon a time in a world far far away'.truncate(27) 
"Once upon a time in a wo..." 

'Once upon a time in a world far far away'.truncate(27, separator: ' ') 
"Once upon a time in a..." 

'Once upon a time in a world far far away'.truncate(27, separator: /\s/) 
"Once upon a time in a..." 

'And they found that many people were sleeping better.'.truncate(25, omission: '... (continued)') 
"And they f... (continued)" 

Sie dieses Beispiel sehen

truncate

Verwandte Themen