Octavian Rasnita wrote at Thu, 18 Jul 2002 15:51:45 +0200:

> I have a string $line that can be a short word or a very long line, and I want to 
>cut it if it is
> longer than 20 characters, then to end it with .... Is there any Perl function for 
>chopping what
> is over a number of characters?
> 
> For example I have a string:
> 
> $line = "Microsoft is a big company and I love Bill Gates"; And I want to cut it to 
>become:
> 
> $line = "Microsoft is a big...";

One way could be to use a regex:

$line =~ s/(.{0,20})(.*)/$1 . ($2 ? "..." : "")/e;

Another way could use a a substr:

for ($line) {
   last unless length > 20;
   $_ = substr $_, 0, 20;
   $_ .= "...";
}


Greetings,
Janek


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to