Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: In search of a better way to trim string

by Jasper (Chaplain)
on Jul 19, 2004 at 10:51 UTC ( #375519=note: print w/ replies, xml ) Need Help??


in reply to In search of a better way to trim string

Alternative with regex:

sub trimTo { my ($line, $length) = @_; $line =~ s/^(.{0,$length}\s|.{$length}).*/$1.../; return $line; }
I think has the same behaviour as the others. Not tested fully. Not that I think it's any faster than any substr method, but it's an alternative.


Comment on Re: In search of a better way to trim string
Download Code
Re^2: In search of a better way to trim string
by kiat (Vicar) on Jul 19, 2004 at 11:30 UTC
    Maybe just add a line to return the string when its length is shorter than the desired length. Otherwise, with a string like "this is a very long sentence", you get "this is a very long ..." instead of "this is a very long sentence".

    Thanks and cheers :)

      Yes, sorry about that. I'm sure there's a one character change that makes the regex not match when it's shorter than $length, but a) I can't see it immediately, b) that's just stupid golfing.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://375519]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (11)
As of 2014-04-23 19:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (553 votes), past polls