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

How do I insert characters at certain positions in a string?

by Anonymous Monk
on Jun 07, 2000 at 22:41 UTC ( #16931=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: (strings)

Example :
In the string "     " (5 chars long), add "A" at character 3 and "B" at character 2 to result in " BA  ".

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I insert characters at certain positions in a string?
by Anonymous Monk on Sep 16, 2003 at 22:28 UTC
    If you do not want to replace the character you can set the substring length to zero.
    my $padded = 'ABCDEFG'; substr($padded, 2, 0) = '1'; substr($padded, 1, 0) = '2';
    This will result in 'A2B1CDEFG'
Re: How do I insert characters at certain positions in a string?
by mdillon (Priest) on Jun 07, 2000 at 22:47 UTC
    my $padded = ' ' x 5; substr($padded, 2, 1) = 'A'; substr($padded, 1, 1) = 'B';
    Will result in: ' BA  '

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2021-10-27 08:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (91 votes). Check out past polls.

    Notices?