Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Truncate string when it is too long

Tags:

string

ruby

I have two strings:

short_string = "hello world"
long_string = "this is a very long long long .... string" # suppose more than 10000 chars

I want to change the default behavior of print to:

puts short_string
# => "hello world"
puts long_string
# => "this is a very long long....."

The long_string is only partially printed. I tried to change String#to_s, but it didn't work. Does anyone know how to do it like this?

updated

Actually i wanna it works smoothly, that means the following cases also work fine:

> puts very_long_str
> puts [very_long_str]
> puts {:a => very_long_str}

So i think the behavior belongs to String.

Thanks all anyway.

like image 957
crax Avatar asked Sep 27 '13 09:09

crax


People also ask

How do you trim a long string?

If it's longer than a given length n , clip it to length n ( substr or slice ) and add html entity &hellip; (…) to the clipped string. function truncate( str, n, useWordBoundary ){ if (str. length <= n) { return str; } const subString = str. slice(0, n-1); // the original check return (useWordBoundary ?

How do you truncate a long string in python?

The other way to truncate a string is to use a rsplit() python function. rsplit() function takes the string, a delimiter value to split the string into parts, and it returns a list of words contained in the string split by the provided delimiter.

How do you shorten a string?

Make a loop at the end of the string. After cutting the string at the proper length, take the end of the string and tie a knot at the very end, then fold the string over and tie a loop, about the same size as the original loop (about 2cm in diameter).

How do you truncate a paragraph to 100 characters in HTML?

JavaScript Code: text_truncate = function(str, length, ending) { if (length == null) { length = 100; } if (ending == null) { ending = '...'; } if (str. length > length) { return str. substring(0, length - ending. length) + ending; } else { return str; } }; console.


2 Answers

First of all, you need a method to truncate a string, either something like:

def truncate(string, max)
  string.length > max ? "#{string[0...max]}..." : string
end

Or by extending String: (it's not recommended to alter core classes, though)

class String
  def truncate(max)
    length > max ? "#{self[0...max]}..." : self
  end
end

Now you can call truncate when printing the string:

puts "short string".truncate
#=> short string

puts "a very, very, very, very long string".truncate
#=> a very, very, very, ...

Or you could just define your own puts:

def puts(string)
  super(string.truncate(20))
end

puts "short string"
#=> short string

puts "a very, very, very, very long string"
#=> a very, very, very, ...

Note that Kernel#puts takes a variable number of arguments, you might want to change your puts method accordingly.

like image 189
Stefan Avatar answered Nov 01 '22 22:11

Stefan


This is how Ruby on Rails does it in their String#truncate method as a monkey-patch:

class String
  def truncate(truncate_at, options = {})
    return dup unless length > truncate_at

    options[:omission] ||= '...'
    length_with_room_for_omission = truncate_at - options[:omission].length
    stop = if options[:separator]
      rindex(options[:separator], length_with_room_for_omission) || 
        length_with_room_for_omission
      else
        length_with_room_for_omission
      end

    "#{self[0...stop]}#{options[:omission]}"
  end
end

Then you can use it like this

'And they found that many people were sleeping better.'.truncate(25, omission: '... (continued)')
# => "And they f... (continued)"
like image 25
Oto Brglez Avatar answered Nov 01 '22 22:11

Oto Brglez