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 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 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 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!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
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
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
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?
|
|
Chatterbox?
|
How do I use this? | Other CB clients
|
Other Users?
|
Others scrutinizing the Monastery: (7) As of 2021-02-27 13:11 GMT
|
Sections?
|
|
Information?
|
|
Find Nodes?
|
|
Leftovers?
|
|
Voting Booth?
|
No recent polls found
|
Notices?
|
|
|