CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
First 4 characters of a string


New User

Feb 16, 2002, 6:46 PM

Post #1 of 2 (1128 views)
First 4 characters of a string Can't Post

I have a variable like


I would like to get from this variable only the first 4 characters:


How could I do this?

Administrator / Moderator

Feb 16, 2002, 8:00 PM

Post #2 of 2 (1126 views)
Re: [Alain] First 4 characters of a string [In reply to] Can't Post

[url=]substr is your friend. It will allow you to extract any number of characters from a string.

It works like this:

#                           start at, stop at, replace with (if anything) 
my $first_4 = substr $var, 0, 4, 'newtext';

In your scenario, you can use substr like this to get what you need:

[perl]my $var = "21f2.albumimage.jpg";

my $first_4 = substr $var, 0, 4;

print $first_4;[/perl]


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives