Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 718
  • 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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