#/usr/bin/perl use strict; use constant TRUE => 1; use constant FALSE => 0; our $debug = FALSE; print truncate_string(" abcdefg ",256); sub truncate_string{ my ($string,$trunc_length) = @_; $string =~ m/^(\s*)(.*?)(\s*)$/; my $space_count_pre = length($1); my $letter_count = length($2); my $space_text_length = $space_count_pre + $letter_count; sub debug{ print "Beginning Spaces: $space_count_pre\n"; print "Text Length: $letter_count\n"; print "Trunc Length: $trunc_length\n"; print "String: \"$string\"\n\n"; } debug() if $debug; # if the final string is already less than the current number of + alphanumeric characters if ($trunc_length <= $letter_count){$string = substr($2,0,$trunc +_length);return $string;} # if the final string is bigger than the beginning spaces + text if ($trunc_length > $space_text_length) {$string = "$1$2" . subs +tr($3,0,$trunc_length - $space_text_length);return $string;} # if the final string is bigger than text, but smaller than begi +nning spaces + text if ($trunc_length > $letter_count && $trunc_length < $space_text +_length){$string = substr($1,0,$trunc_length - $space_text_length) . +$2;return $string;} }
In reply to Re: Truncate string to limited length, throwing away unimportant characters first.
by deMize
in thread Truncate string to limited length, throwing away unimportant characters first.
by ambrus
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |