Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I center truncate a string?

Tags:

Does anybody have any code handy that center truncates a string in Ruby on Rails?

Something like this: Ex: "Hello World, how are you?" => "Hel...you?"

like image 363
chuboy Avatar asked Sep 20 '09 16:09

chuboy


People also ask

How do you truncate a string?

Truncate the string (first argument) if it is longer than the given maximum string length (second argument) and return the truncated string with a ... ending. The inserted three dots at the end should also add to the string length.

How do you truncate text in Java?

Using String's split() Method. Another way to truncate a String is to use the split() method, which uses a regular expression to split the String into pieces. The first element of results will either be our truncated String, or the original String if length was longer than text.

How do you truncate words in CSS?

Solution # 1: Truncate text for single line This solution works for single-line truncation. Force text to be on a single line: white-space: nowrap; Now, our text should be on the same line and should overflow from the box if it's long enough and wrapped before.


1 Answers

If you want a certain fixed length irrespective of the length of the string, you can use Rails #truncate:

s.truncate(100, omission: "...#{s.last(50)}")
like image 145
Benjamin Sullivan Avatar answered Nov 05 '22 05:11

Benjamin Sullivan