0

Let string_a = "I'm a string but this aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa is long."

How is it possible to detect the aaaa...aaa (the part without spaces that is very long) part and truncate it so that the result looks like (the rest of the string should look the same):

puts string_a # => "I'm a string but this aaa....aaa is long."

I use this method to truncate:

  def truncate_string(string_x)
    splits = string_x.scan(/(.{0,9})(.*?)(.{0,9}$)/)[0]
    splits[1] = "..." if splits[1].length > 3
    splits.join
  end

So running:

puts truncate_string("I'm a very long long long string") # => "I'm a ver...ng string"

The problem is detecting the 'aaaa...aaaa' and apply truncate_string to it.

First part of the solution? Detecting strings that are longer than N using regex?

Alex
  • 2,036
  • 4
  • 22
  • 31
  • What do you want to detect, exactly? Any long sequence of repeated characters? – fgp Aug 31 '12 at 18:08
  • Detecting a string that is longer or equal to N. N can be any integer. – Alex Aug 31 '12 at 18:08
  • You mean a string or a sequence of characters between two whitespaces? To detect if a string has more than N characters you just need to check it's size, which I believe is not your issue... – fgp Aug 31 '12 at 18:11
  • Sorry I didn't mention that the string to detect should be without spaces. So it's a sequence of characters between two whitespaces. – Alex Aug 31 '12 at 18:13

2 Answers2

2

What about something like

string.gsub(/\S{10,}/) { |x| "#{x[0..3]}...#{x[-3..-1]}" }

where 10 is the maximum length of a word?

christianblais
  • 2,448
  • 17
  • 14
1

how do you like this?

s = "I'm a string but this aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa is long."
s.gsub(/(.* \w{3})\w{5,}(\w{3}.*)/, '\1...\2')
 => "I'm a string but this aaa...aaa is long." 
phoet
  • 18,688
  • 4
  • 46
  • 74
  • Thanks for your answer but it doesn't work with this example: `"I'm a very long http://www.exemple.com/a/b/c/d/e/f/g/h/1:2:3_3_980__eee long long link"` – Alex Aug 31 '12 at 18:21
  • 2
    You just need to change the \w to a \S. \w means any word character (letter, number, underscore), while \S means any non-whitespace character (so it will include your "/" and ":"). – christianblais Aug 31 '12 at 18:25
  • @Alex learn yourself some regular expressions. it's worth it! – phoet Sep 01 '12 at 05:07