Solved

perl, shorten the text string

Posted on 2010-11-18
3
654 Views
Last Modified: 2012-05-10
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
Comment
Question by:rgbcof
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 10

Accepted Solution

by:
jeromee earned 125 total points
ID: 34166173
$string = substr($string,0,20).'...' if length($string) > 20;
0
 
LVL 12

Expert Comment

by:tel2
ID: 34167716
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
 

Author Closing Comment

by:rgbcof
ID: 34168725
Works well.  Thanks
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

689 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question