develooper Front page | perl.beginners | Postings from February 2002

Re: Crop string to x characters

Thread Previous | Thread Next
From:
Jenda Krynicky
Date:
February 18, 2002 03:26
Subject:
Re: Crop string to x characters
Message ID:
3C70F2E9.32359.32A7F0F@localhost
From:           	"Gary Hawkins" <ghawk@eskimo.com>

> How do I truncate a string to a particular number of characters?
> 
> This is expensive:
> 
> $shortdescription =~
> s/(...................................................................
> ......... ........................................ ).*/$1/;
> 
> Gary

If you insisted on using regexps you could use

	$shortdescription =~ s/^.{50}.*$/$1/;

But it would be quicker to use substr()

	$shortdescription = substr $shortdescription, 0, 50;

Jenda

=========== Jenda@Krynicky.cz == http://Jenda.Krynicky.cz ==========
There is a reason for living. There must be. I've seen it somewhere.
It's just that in the mess on my table ... and in my brain.
I can't find it.
					--- me

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About