my $maxlen = 20; my $longtext = "This is some very long string that needs to be truncated to $maxlen characters..."; my $trunctext = substr( $longtext, 0, rindex( $longtext, " ", $maxlen )); print "$longtext\n$trunctext\n";