Substring/Top and tail: Difference between revisions
({{header|Lua}}) |
|||
Line 6: | Line 6: | ||
* String with both the first and last characters removed |
* String with both the first and last characters removed |
||
=={{header|ALGOL 68}}== |
|||
{{trans|AWK}} |
|||
{{works with|ALGOL 68|Revision 1 - no extensions to language used.}} |
|||
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny].}} |
|||
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}} |
|||
<lang algol68>#!/usr/local/bin/a68g --script # |
|||
STRING my string="knight"; |
|||
printf(($gl$, |
|||
my string[2:], # remove the first letter # |
|||
my string[ :UPB my string-1], # remove the last character # |
|||
my string[2:UPB my string-1] # remove both the first and last character # |
|||
))</lang> |
|||
Output: |
|||
<pre> |
|||
night |
|||
knigh |
|||
nigh |
|||
</pre> |
|||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
Revision as of 21:04, 15 June 2011
The task is to demonstrate how to remove the first and last characters from a string. The solution should demonstrate how to obtain the following results:
- String with first character removed
- String with last character removed
- String with both the first and last characters removed
ALGOL 68
<lang algol68>#!/usr/local/bin/a68g --script #
STRING my string="knight"; printf(($gl$,
my string[2:], # remove the first letter # my string[ :UPB my string-1], # remove the last character # my string[2:UPB my string-1] # remove both the first and last character #
))</lang> Output:
night knigh nigh
AWK
<lang awk>BEGIN {
mystring="knight" print substr(mystring,2) # remove the first letter print substr(mystring,1,length(mystring)-1) # remove the last character print substr(mystring,2,length(mystring)-1) # remove both the first and last character
}</lang>
Icon and Unicon
The task is accomplished by sub-stringing. <lang Icon>procedure main() write(s := "knight"," --> ", s[2:0]) # drop 1st char write(s := "sock"," --> ", s[1:-1]) # drop last write(s := "brooms"," --> ", s[2:-1]) # drop both end</lang>
It could also be accomplished (less clearly) by assigning into the string as below. Very awkward for both front and back. <lang Icon>write(s := "knight"," --> ", s[1] := "", s) # drop 1st char</lang>
J
The monadic primitives }.
(Behead) and }:
(Curtail) are useful for this task.
Example use:
<lang j> }. 'knight' NB. drop first item
night
}: 'socks' NB. drop last item
sock
}: }. 'brooms' NB. drop first and last items
room</lang>
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>
Lua
<lang lua>print (string.sub("knights",2)) -- remove the first character print (string.sub("knights",1,-2)) -- remove the last character print (string.sub("knights",2,-2)) -- remove the first and last characters</lang>
PARI/GP
<lang parigp>df(s)=concat(vecextract(Vec(s),1<<#s-2)); dl(s)=concat(vecextract(Vec(s),1<<(#s-1)-1)); db(s)=concat(vecextract(Vec(s),1<<(#s-1)-2));</lang>
Perl
<lang perl>print substr("knight",1), "\n"; # strip first character print substr("socks", 0, -1), "\n"; # strip last character print substr("brooms", 1, -1), "\n"; # strip both first and last characters</lang>
In perl, we can also remove the last character from a string variable with the chop function:
<lang perl>$string = 'ouch'; $bits = chop($string); # The last letter is returned by the chop function print $bits; # h print $string; # ouc # See we really did chop the last letter off</lang>
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>
PicoLisp
<lang PicoLisp>: (pack (cdr (chop "knight"))) # Remove first character -> "night"
- (pack (head -1 (chop "socks"))) # Remove last character
-> "sock"
- (pack (cddr (rot (chop "brooms")))) # Remove first and last characters
-> "room"</lang>
Prolog
Works with SWI-Prolog.
<lang Prolog>remove_first_last_chars :- L = "Rosetta", L = [_|L1], remove_last(L, L2), remove_last(L1, L3), writef('Original string : %s\n', [L]), writef('Without first char : %s\n', [L1]), writef('Without last char : %s\n', [L2]), writef('Without first/last chars : %s\n', [L3]).
remove_last(L, LR) :- reverse(L, [_ | L1]), reverse(L1, LR).</lang> Output :
?- remove_first_last_chars. Original string : Rosetta Without first char : osetta Without last char : Rosett Without first/last chars : osett true.
PureBasic
<lang PureBasic>If OpenConsole()
PrintN(Right("knight", Len("knight") - 1)) ;strip the first letter PrintN(Left("socks", Len("socks")- 1)) ;strip the last letter PrintN(Mid("brooms", 2, Len("brooms") - 2)) ;strip both the first and last letter Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input() CloseConsole()
EndIf</lang> Sample output:
night sock room
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>
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>
ZX Spectrum Basic
<lang zxbasic>10 PRINT FN f$("knight"): REM strip the first letter 20 PRINT FN l$("socks"): REM strip the last letter 30 PRINT FN b$("brooms"): REM strip both the first and last letter 100 STOP
9000 DEF FN f$(a$)=a$(2 TO LEN(a$)) 9010 DEF FN l$(a$)=a$(1 TO LEN(a$)-(1 AND (LEN(a$)>=1))) 9020 DEF FN b$(a$)=FN l$(FN f$(a$)) </lang>