[Israel.pm] how do i cut the first 2 characters/digits of a string?

Shlomi Fish shlomif at iglu.org.il
Thu Jul 15 03:47:49 PDT 2004


On Thursday 15 July 2004 12:09, Shlomo Yona wrote:
> On Thu, 15 Jul 2004, Ernst, Yehuda wrote:
>
>
> my $string = 'how do i cut the first 2 characters/digits of a string?'
> my $cut_string = substr $string,2;
>
> this, however simply copies all characters of $string to
> $cut_string except for the first two.
>
>
> You can do it in place using a regular expression:
>
> $string=~s/^..(.*)$/$1/;
> or if you only want to cure the first two digits:
> $string=~s/^\d\d(.*)$/$1/;
>
> you might need to add the s modifier, depending on the
> content of the string (if you want . to match newlines as
> well).

You can also do it in place using "substr":

<<<
$mystring="abcdefgh";
substr($mystring,0,2)="";
>>>

As substr returns an lvalue.

There's more than one way to do it.

Regards,

	Shlomi Fish

-- 

---------------------------------------------------------------------
Shlomi Fish      shlomif at iglu.org.il
Homepage:        http://shlomif.il.eu.org/

Knuth is not God! It took him two days to build the Roman Empire.



More information about the Perl mailing list