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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



Alain
New User

Feb 16, 2002, 6:46 PM

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

I have a variable like

$var="21f2.albumimage.jpg"

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

$first_4_characters="21f2"

How could I do this?


Jasmine
Administrator / Moderator

Feb 16, 2002, 8:00 PM

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

[url=http://www.perldoc.com/perl5.6.1/pod/func/substr.html]substr is your friend. It will allow you to extract any number of characters from a string.

It works like this:


Code
#                           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