Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: How do I pull n characters off the front of a string?

by davido (Cardinal)
on Oct 03, 2003 at 01:31 UTC ( [id://296137]=note: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    my $string = "1234567890abcdefghijABCDEFGHIJK";
    my $chars;
    my $n = 2;
    
  2. or download this
    $chars = substr( $string, 0, $n );
    substr( $string, 0, $n ) = "";
    
  3. or download this
    ( $chars, $string ) = ( substr($string,0,$n), substr($string,$n) );
    
  4. or download this
    $chars = substr ($string, 0, $n, "");
    
  5. or download this
    $string = s/^(.{$n})(.*)$/$2/s;
    $chars = $1;
    
  6. or download this
    ($chars, $string) = split /^(.{$n})/, $string, 1;
    
  7. or download this
    ($chars, $string) = $string =~ /^(.{$n})(.*)$/s;
    
  8. or download this
    ( $chars, $string ) = unpack "a$n a@{[length($string)-$n]}", $string;
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://296137]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-16 09:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found