in reply to how to shorten given string?
Unless I'm missing something important, this should cut the line at the right spot. Note that it's 20+1 because the length has to be max length + 1 so it won't cut a word that fits entirely into the string.my $str = '1234 6789 1234 6789 1234 6789 '; my $printstr = substr($str, 0, 20+1); $printstr =~ s/\W+\w*$//; print $printstr;
EDIT: You may have to change the regex if words can include characters other than \w, such as .
|
|---|