Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: In search of a better way to trim string length

by tachyon (Chancellor)
on Jul 19, 2004 at 08:09 UTC ( #375481=note: print w/replies, xml ) Need Help??

in reply to In search of a better way to trim string

You can return immediately if the string is less than or equal to the desired length. Then it depends on what you want to do to make it pretty. I have a couple of routines like this:

sub shorten_string { my ( $string, $length ) = @_; $length ||= 64; $length = 10 if $length < 10; return $string unless length($string) > $length; my $chunk = int($length/2)-3; return substr($string,0,$chunk+1) . ' ... ' . substr($string,-$chunk +); } sub shorten_url { my ( $url, $length ) = @_; $length ||= 64; return $url unless $url and length($url) > $length; ( $url ) = split /\?/, $url; return $url if length($url) < $length or $url =~ m!\w+://[^/]+/?$!; $url =~ s!(\w+://[^/]+)!!; my $domain = $1 ? "$1/ ... " : '... '; $length -= length $domain; return $domain if $length < 1; my @bits = split '/', $url; my @keepers = $url =~ m!/$! ? ('') : (); my $tail = 1; while ( my $bit = pop @bits ) { next unless $bit; $length -= (length($bit) + 1); unshift @keepers, $bit if $tail or $length > 0; $tail = 0; last if $length < 1; } return @keepers ? ( "$domain/".join'/',@keepers ) : $domain; }



Replies are listed 'Best First'.
Re^2: In search of a better way to trim string length
by Vennis (Pilgrim) on Jul 19, 2004 at 12:19 UTC
    sub trimTo { my ($line, $length) = @_; $line=~s/([^ \s]{$length})([^ \s])/$1...\n$2/g; return $line; }
    Though only the first line is 30, and the rest is 31 chars, i think it's more or less doing what you search for ?
    It also makes sure strings that are exact 30 length dont have dots added.

    I just saw Jaspers solution, it's a bit different, i thought i post it anyway.

    Code isn't doing what was expected. Sorry Kiat :-)

      Hm...yours doesn't produce the desired outputs.

      Given the following inputs:

      1) "thisisthelongesttopicthatwilleverbepostedinperlmonks"

      2) "this is the longest topic that will ever be posted in perlmonks"

      Desired outputs:

      1a) thisisthelongesttopicthatwille ...

      2a) this is the longest topic ...

      Yours gave the following outputs:

      1a) thisisthelongesttopicthatwille...

      2a) this is the longest topic that will ever be posted in perlmonks

      Did I miss something?

        Nope, you missed nothing, i missed something :-) Never mind that code then, i'll leave it in case somebody finds it either useful or inspiring.
Re^2: In search of a better way to trim string length
by kiat (Vicar) on Jul 19, 2004 at 08:38 UTC
    Thanks, tachyon!

    Interesting! I like the idea of breaking the string up at the middle with '...'.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://375481]
[ambrus]: Something you can have at home and your corner print shop, without a whole printing press's worth of equipment.
[ambrus]: As for TeX, I'm not trying to discourage anyone from writing carefully beautifully typeset documents, in maths or outside. But most people aren't willing to do that, and will spend only little time about the formatting,
[ambrus]: and try to leave everything else to automated systems without checking how what they write came out format-wise, and for those people, discounting the part about journals with a specific format above,
[ambrus]: just blindly recommending to use LaTeX is a bad idea now.

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (10)
As of 2017-09-26 11:18 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (293 votes). Check out past polls.