Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

indexing a string

Status
Not open for further replies.

zzzqqq

Programmer
Oct 12, 2005
17
IE
Hi,
Anyone know is it possible to insert a string value at a specific index in another string?
For example insert "234" into "657878768" at index 5 therefore overwriting it?
Cheers,
Mark.
 
Use substr:
Code:
my $original = '657878768';
my $replacement = '234';

# where in the string you want to insert it
my $index = 5;

# how many characters you want to replace
my $replace = length $replacement;

substr( $original, $index, $replace, $replacement );
 
Cheers,
Excellent! Had a fair idea of that was really wondering was there a direct indexing method of doing it?
Thanks
 
Do you mean using substr() as an lvalue?
Code:
substr($original, $index, $replace) =  $replacement;

- Andrew
Text::Highlight - A language-neutral syntax highlighting module in Perl
also on SourceForge including demo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top