Replace function based on substr function : Replace « String « Perl






Replace function based on substr function

   

sub replace
{
    ($text, $to_replace, $replace_with) = @_;

    substr ($text, index($text, $to_replace),
        length($to_replace), $replace_with);

    return $text;
}

print replace("Here is the text.", "text", "word");

   
    
    
  








Related examples in the same category

1.Replace first 5 characters of $string with 'bye' and assign substring that was replced to $substring
2.Replace function based on the length