Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

How do I insert, (not overwrite) into a string?

( #29236=categorized question: print w/ replies, xml ) Need Help??
Contributed by Anonymous Monk on Aug 23, 2000 at 19:59 UTC
Q&A  > strings


Description:

eg, insert "H" at the begining of a string, without loosing the 0th element "ello" -> "Hello"

Answer: How do I insert, (not overwrite) into a string?
contributed by KM

Another way to do it:

my $str = "ello"; substr($str,0,0,"H");

Cheers,
KM

Answer: How do I insert, (not overwrite) into a string?
contributed by Shendal

my($str) = 'ello'; $str = "H$str";
Answer: How do I insert, (not overwrite) into a string?
contributed by KM

Did a quick benchmark out of curiosity:

KM:     substr($_,0,0,"H")
Ovid:    s/^(.*)$/H$1/
Shendal:  $_ .= "H".$_
turnstep: s/^/H/

Benchmark: timing 100000 iterations of KM, Ovid, Shendal, turnstep... KM: -1 wallclock secs ( 0.55 usr + 0.00 sys = 0.55 CPU) Ovid: 2 wallclock secs ( 2.20 usr + 0.00 sys = 2.20 CPU) Shendal: 0 wallclock secs ( 0.53 usr + 0.00 sys = 0.53 CPU) turnstep: 1 wallclock secs ( 0.98 usr + 0.00 sys = 0.98 CPU)

Cheers,
KM

Answer: How do I insert, (not overwrite) into a string?
contributed by turnstep

Regular expressions can handle this just fine as well:

my $string = "ello"; $string =~ s/^/Henry said H/;
Answer: How do I insert, (not overwrite) into a string?
contributed by davorg

my $str = 'ello'; my $char = 'H'; print $str, "\n"; substr($str, 0, 0, $char); print $str, "\n";
Answer: How do I insert, (not overwrite) into a string?
contributed by Ovid

There are several methods to do this. Shendal has listed one. Here are a couple of others:

# Concatenation my $str = 'ello'; $str = 'H' . $str; # Regex my $str = 'ello'; $str =~ s/^(.*)$/H$1/;
Cheers,
Ovid
Answer: How do I insert, (not overwrite) into a string?
contributed by redcloud

Just to follow-up KM and davorg, note that you can use substr() as an lvalue. It's just syntactic sugar, but I like the taste of it. 8^)

use strict; my $str = "ello"; substr($str,0,0) = "H"; print $str, "\n";
Answer: How do I insert, (not overwrite) into a string?
contributed by mphilip1

try this sub I wrote:

# sub signature: # insertXintoYatZ( X , Y , Z ) ; sub insertXintoYatZ{ my ( $X , $Y , $Z ) = @_; substr( $Y , $Z , -length($Y) ) = $X ; return $Y; } $s = "The black cat climbed the green tree"; print "s = $s \n"; print 'insert = '.($s = insertXintoYatZ( "tall ", $s , 26 ))."\n"; print " (now, s = '$s' ) \n"; # OUTPUT: # s = The black cat climbed the green tree # insert = 'tall ' # (now, s = 'The black cat climbed the tall green tree' )";
Answer: How do I insert, (not overwrite) into a string?
contributed by Fian

I know this is kinda long winded but if u wanna insert into a string split the thing into its constituent characters and bung em in an array then u can insert whatever u want wherever u want.....

@bitsOfString = split(//,$yourString);

Slán

Fian. Edited by davido: Added code tags.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • 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 surveying the Monastery: (7)
    As of 2014-08-23 03:51 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (172 votes), past polls