Substring/Top and tail: Difference between revisions
Content deleted Content added
→{{header|SNOBOL4}}: Added Standard ML |
→{{header|Perl 6}}: update to modern Perl 6; demo Unicode plane independence |
||
Line 780: | Line 780: | ||
=={{header|Perl 6}}== |
=={{header|Perl 6}}== |
||
Perl 6 provides both functional and method forms of substr. Note that, unlike in Perl 5, offsets from the end do not use negative numbers, but instead require a function expressing the negative offset relative to the length parameter, which is supplied by the operator. The form <tt>*-1</tt> is just a simple way to write such a function. |
|||
Perl 6 has a substr routine similar to that of Perl. The only real difference is that it may be called as a subroutine or as a method. |
|||
We use musical sharps and flats to illustrate that Perl is comfortable with characters from any Unicode plane. |
|||
<lang perl6>say substr('knight', 1); # strip first character - sub |
|||
say 'knight'.substr(1); # strip first character - method |
|||
⚫ | |||
say substr('socks', 0, -1); # strip last character - sub |
|||
say 'socks'.substr( 0, -1); # strip last character - method |
|||
print qq:to/END/; |
|||
say substr('brooms', 1, -1); # strip both first and last characters - sub |
|||
Original: |
|||
say 'brooms'.substr(1, -1); # strip both first and last characters - method</lang> |
|||
$s |
|||
Remove first character: |
|||
Perl 6 also has chop though it works differently from Perl. There is also p5chop that works like Perls chop. |
|||
{ substr($s, 1) } |
|||
{ $s.substr(1) } |
|||
Remove last character: |
|||
⚫ | |||
{ substr($s, 0, *-1) } |
|||
say $string.chop; # ouc - does not modify original $string |
|||
{ $s.substr( 0, *-1) } |
|||
say $string; # ouch |
|||
{ $s.chop } |
|||
say $string.p5chop; # h - returns the character chopped off and modifies $string |
|||
say $string; # ouc</lang> |
|||
Remove first and last characters: |
|||
{ substr($s, 1, *-1) } |
|||
{ $s.substr(1, *-1) } |
|||
END</lang> |
|||
{{out}} |
|||
<pre>Original: |
|||
𝄪♯♮♭𝄫 |
|||
Remove first character: |
|||
♯♮♭𝄫 |
|||
♯♮♭𝄫 |
|||
Remove last character: |
|||
𝄪♯♮♭ |
|||
𝄪♯♮♭ |
|||
𝄪♯♮♭ |
|||
Remove first and last characters: |
|||
♯♮♭ |
|||
♯♮♭</pre> |
|||
=={{header|PHP}}== |
=={{header|PHP}}== |