mosh has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I'd like to remove a substring " \'\' " from string and return the string without the substring.
the string is:
'\'\'abcdef'
if I use substr command I will get the substring and not the string.
Any elegant solution ?
Mosh.

Replies are listed 'Best First'.
Re: Remove substring
by Ido (Hermit) on Jun 26, 2005 at 15:25 UTC
    If I understand the question correctly, you use:
    substr($string,0,2,"");
    Which removes what you want, but also returns it, and not the remaining string. Instead, try using substr as a lvalue:
    substr($string,0,2)="";

      Just to add to this answer, both of those solutions will alter the original string. If you do not want the original string to be altered, then make a copy first...

Re: Remove substring
by davido (Cardinal) on Jun 26, 2005 at 16:55 UTC

    That's easily done with substitution:

    $string =~ s/\\'\\'//;

    Or more elegantly...

    $string =~ s/\Q\'\'\E//;

    ...where \Q and \E eliminate the need to escape the backslashes. Actually, for such a small substring, \Q and \E may just look like too much line noise.

    This will remove the first occurrence. If there are multiple, you have to modify with /g.


    Dave

Re: Remove substring
by tlm (Prior) on Jun 26, 2005 at 16:47 UTC

    I am puzzled by the question. If $x is the string 'abcdefg', then

    my $tail = substr $x, 2;
    will assign to $tail the string 'cdefg', which sounds like what you want.

    the lowliest monk