• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 689
  • Last Modified:

perl, shorten the text string

my $longString = "Los Angeles, California, United States of America";

If  a string is more than 20 character, I want to replace the rest with "...", so that $longString will be "Los Angeles, Califor ...";
0
rgbcof
Asked:
rgbcof
1 Solution
 
jeromeeCommented:
$string = substr($string,0,20).'...' if length($string) > 20;
0
 
tel2Commented:
Hi rgbcof,

You could do this:

    $longString =~ s/^(.{20}).+/$1.../;

or if you want a space before the dots, as in your example, do this:

    $longString =~ s/^(.{20}).+/$1 .../;
0
 
rgbcofAuthor Commented:
Works well.  Thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now