Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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

( #75128=categorized answer: print w/ replies, xml ) Need Help??

Q&A > strings > How do I pull n characters off the front of a string? 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

Comment on Answer: How do I pull n characters off the front of a string?
Select or Download Code
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (12)
As of 2014-09-16 16:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (33 votes), past polls