Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

truncating a line

by Anonymous Monk
on Jul 01, 2003 at 13:48 UTC ( #270462=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks :-)

I have strings of varying length. I would like to find out the length, and remove all characters after the 17th. Then when I have a string of 17 characters, I would like to append '...' to the end of the string.

For example:

Old string: 'hello world, goodbye world'
Chop off after 17 characters: 'hello world, good'
Now append 3 periods: 'hello world, good...'

Any ideas? Thanks,

Replies are listed 'Best First'.
Re: truncating a line
by ctilmes (Vicar) on Jul 01, 2003 at 13:56 UTC
    use the substr() as an lvalue:
    substr($str, 17) = '...' if length $str > 17;
Re: truncating a line
by tilly (Archbishop) on Jul 01, 2003 at 13:52 UTC
    Use length to find the length, and then substr to chop it.

    BTW a tip. It works slightly more smoothly if you only chop and add the dots if the string is over 20 characters.

      BTW a tip. It works slightly more smoothly if you only chop and add the dots if the string is over 20 characters.
      What exactly do you mean by this?
      is there a technical reason,
      or because it cuts a word in half with that particular string,
      or in your experience 20 chars is best to still leave the string reasonably understandable?

        Because truncating and adding 3 dots leaves the length of the string at 20 chars. Why bother truncating and indicating a continuation unless you are going to wind up with a shorter string for the loss of information?

        Trust me. This is a detail that users tend to notice.

Re: truncating a line
by dreadpiratepeter (Priest) on Jul 01, 2003 at 13:51 UTC
    $str = length($str)>17 ? substr($str,0,17).'...' : $str;

    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."
Re: truncating a line
by particle (Vicar) on Jul 01, 2003 at 13:55 UTC

    you'll want to use substr:

    my $long_line= 'this line is more than seventeen characters.'; my $short_line= substr( $long_line, 16, length $long_line ) .= '...';

    ~Particle *accelerates*

Re: truncating a line
by BrowserUk (Pope) on Jul 01, 2003 at 14:16 UTC


    $line = $1 . '...' if $line =~ m[^(.{17})];

    ...but ctilmes method above is the right way:)

    Update: Corrected (another) typo pointed out by sauoq .

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

Re: truncating a line
by hardburn (Abbot) on Jul 01, 2003 at 14:17 UTC

    Just to be different:

    # $str defined elsewhere $str =~ s/\A (.{0,17}) (.*) # Normal rules about .* don't apply--we # really do need everything that's left \z/$1(?($2) \. \. \.)/x;

    Update: Never mind. Doesn't work.

    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Re: truncating a line
by svsingh (Priest) on Jul 01, 2003 at 15:26 UTC
    I haven't seen anyone answer with this one yet, so I thought I'd throw it out there. Sorry if it's a repeat. I'm not as proficient with all the regex tricks as I'd like to be.
    $line =~ /^(.{0,17})/; print "$1\n";

    If that's a rehash, then please let me know. I think it's close to BrowserUK's answer, but they have an if that mine doesn't. Thanks.

      Removing an if is only worthwhile if you still have a correct solution.

      Yours does not add the requested "..." when it is needed. (And like the other RE solution will also mess up if there is a newline in there.)

        Argh. Thanks tilly. I was so jazzed that I knew how to approach the first half, I overlooked the second. Read the spec, read the spec, read the spec.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://270462]
Approved by tilly
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2021-05-17 13:40 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (156 votes). Check out past polls.