Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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

by xtrem (Novice)
on Apr 24, 2001 at 20:29 UTC ( #75128=categorized answer: print w/replies, xml ) Need Help??

Q&A > strings > How do I pull n characters off the front of a string? - Answer contributed by xtrem

my ($x,$y) = ($string =~ /^(.{n})(.{n})/);
This will put the 1st n char into $x and the 2nd n chars into $y without touching $string... if you want the rest of the chars:
my ($x, $y, $rest) = ($string =~ /^(.{n})(.{n})(.*)/);
if you don't like regexp's you can allways try substr

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
[Eily]: :P, the search box is on the top left, or you can use Super Search
[Corion]: jthebarg: There is the switch keyword if you want that, but maybe a dispatch table is closer.
[Corion]: If by "case" you meant "case-insensitive ", see fc, uc and lc and perlop on the /i flag ;)
Corion waves to virtualsue
[virtualsue]: I have made a post for the first time in ... years
[marto]: hey virtualsue!

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (15)
As of 2017-09-26 13:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (295 votes). Check out past polls.

    Notices?