Substring/Top and tail: Difference between revisions

added js, php, python, ruby
m (Minor tidying)
(added js, php, python, ruby)
Line 5:
* String with last character removed
* String with both the first and last characters removed
 
=={{header|JavaScript}}==
 
<lang javascript>alert("knight".slice(1)); // strip first character
alert("socks".slice(0, -1)); // strip last character
alert("brooms".slice(1, -1)); // strip both first and last characters</lang>
 
=={{header|Perl}}==
Line 18 ⟶ 24:
print $bits; # h
print $string; # ouc # See we really did chop the last letter off</lang>
 
=={{header|PHP}}==
 
<lang php><?php
echo substr("knight", 1), "\n"; // strip first character
echo substr("socks", 0, -1), "\n"; // strip last character
echo substr("brooms", 1, -1), "\n"; // strip both first and last characters
?></lang>
 
=={{header|PureBasic}}==
Line 32 ⟶ 46:
sock
room</pre>
 
=={{header|Python}}==
 
<lang python>print "knight"[1:] # strip first character
print "socks"[:-1] # strip last character
print "brooms"[1:-1] # strip both first and last characters</lang>
 
=={{header|Ruby}}==
 
<lang ruby>puts "knight"[1..-1] # strip first character
puts "socks"[0..-2] # strip last character
puts "socks".chop # alternate way to strip last character
puts "brooms"[1..-2] # strip both first and last characters</lang>
 
=={{header|ZX Spectrum Basic}}==
Anonymous user