Solved

perl, shorten the text string

Posted on 2010-11-18
3
604 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
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 11

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now