Substring/Top and tail: Difference between revisions
(Ada version added) |
(Go solution) |
||
Line 127: | Line 127: | ||
puts(1, strip_last("write")) -- strip last character |
puts(1, strip_last("write")) -- strip last character |
||
puts(1, strip_both("brooms")) -- strip both first and last characters</lang> |
puts(1, strip_both("brooms")) -- strip both first and last characters</lang> |
||
=={{header|Go}}== |
|||
Go strings are byte arrays that can hold whatever you want them to hold. Common contents are ASCII and UTF-8. You use different techniques depending on how you are interpreting the string. The utf8 package functions shown here allows efficient extraction of first and last runes without decoding the entire string. |
|||
<lang go>package main |
|||
import ( |
|||
"fmt" |
|||
"utf8" |
|||
) |
|||
func main() { |
|||
// ASCII contents: Interpreting "characters" as bytes. |
|||
s := "ASCII" |
|||
fmt.Println("String: ", s) |
|||
fmt.Println("First byte removed: ", s[1:]) |
|||
fmt.Println("Last byte removed: ", s[:len(s)-1]) |
|||
fmt.Println("First and last removed:", s[1:len(s)-1]) |
|||
// UTF-8 contents: "Characters" as runes (unicode code points) |
|||
u := "Δημοτική" |
|||
fmt.Println("String: ", u) |
|||
_, sizeFirst := utf8.DecodeRuneInString(u) |
|||
fmt.Println("First rune removed: ", u[sizeFirst:]) |
|||
_, sizeLast := utf8.DecodeLastRuneInString(u) |
|||
fmt.Println("Last rune removed: ", u[:len(u)-sizeLast]) |
|||
fmt.Println("First and last removed:", u[sizeFirst:len(u)-sizeLast]) |
|||
}</lang> |
|||
Output: |
|||
<pre> |
|||
String: ASCII |
|||
First byte removed: SCII |
|||
Last byte removed: ASCI |
|||
First and last removed: SCI |
|||
String: Δημοτική |
|||
First rune removed: ημοτική |
|||
Last rune removed: Δημοτικ |
|||
First and last removed: ημοτικ |
|||
</pre> |
|||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
Revision as of 22:12, 5 July 2011
You are encouraged to solve this task according to the task description, using any language you may know.
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
Ada
<lang Ada>with Ada.Text_IO;
procedure Remove_Characters is
S: String := "upraisers"; use Ada.Text_IO;
begin
Put_Line("Full String: """ & S & """"); Put_Line("Without_First: """ & S(S'First+1 .. S'Last) & """"); Put_Line("Without_Last: """ & S(S'First .. S'Last-1) & """"); Put_Line("Without_Both: """ & S(S'First+1 .. S'Last-1) & """");
end Remove_Characters;</lang>
Output:
Full String: "upraisers" Without_First: "praisers" Without_Last: "upraiser" Without_Both: "praiser"
ALGOL 68
<lang algol68>#!/usr/local/bin/a68g --script #
STRING str="upraisers"; printf(($gl$,
str, # remove no characters # str[LWB str+1: ], # remove the first character # str[ :UPB str-1], # remove the last character # str[LWB str+1:UPB str-1], # remove both the first and last character # str[LWB str+2: ], # remove the first 2 characters # str[ :UPB str-2], # remove the last 2 characters # str[LWB str+1:UPB str-2], # remove 1 before and 2 after # str[LWB str+2:UPB str-1], # remove 2 before and one after # str[LWB str+2:UPB str-2] # remove both the first and last 2 characters #
))</lang> Output:
upraisers praisers upraiser praiser raisers upraise praise raiser raise
AWK
<lang awk>BEGIN {
mystring="knights" 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)-2) # remove both the first and last character
}</lang>
C
<lang c>#include <string.h>
- include <stdlib.h>
- include <stdio.h>
int main( int argc, char ** argv ){
const char * str_a = "knight"; const char * str_b = "socks"; const char * str_c = "brooms";
char * new_a = malloc( strlen( str_a ) - 1 ); char * new_b = malloc( strlen( str_b ) - 1 ); char * new_c = malloc( strlen( str_c ) - 2 );
strcpy( new_a, str_a + 1 ); strncpy( new_b, str_b, strlen( str_b ) - 1 ); strncpy( new_c, str_c + 1, strlen( str_c ) - 2 );
printf( "%s\n%s\n%s\n", new_a, new_b, new_c );
free( new_a ); free( new_b ); free( new_c );
return 0;
}</lang>
Result:
night sock room
ANSI C provides little functionality for text manipulation outside of string.h. While a number of libraries for this purpose have been written, this example uses only ANSI C.
D
Version for ASCII strings: <lang d>import std.stdio;
void main() {
writeln("knight"[1..$]); // strip first character writeln("socks"[0..$-1]); // strip last character writeln("brooms"[1..$-1]); // strip both first and last characters
}</lang>
Euphoria
<lang euphoria>function strip_first(sequence s)
return s[2..$]
end function
function strip_last(sequence s)
return s[1..$-1]
end function
function strip_both(sequence s)
return s[2..$-1]
end function
puts(1, strip_first("knight")) -- strip first character puts(1, strip_last("write")) -- strip last character puts(1, strip_both("brooms")) -- strip both first and last characters</lang>
Go
Go strings are byte arrays that can hold whatever you want them to hold. Common contents are ASCII and UTF-8. You use different techniques depending on how you are interpreting the string. The utf8 package functions shown here allows efficient extraction of first and last runes without decoding the entire string. <lang go>package main
import (
"fmt" "utf8"
)
func main() {
// ASCII contents: Interpreting "characters" as bytes. s := "ASCII" fmt.Println("String: ", s) fmt.Println("First byte removed: ", s[1:]) fmt.Println("Last byte removed: ", s[:len(s)-1]) fmt.Println("First and last removed:", s[1:len(s)-1]) // UTF-8 contents: "Characters" as runes (unicode code points) u := "Δημοτική" fmt.Println("String: ", u) _, sizeFirst := utf8.DecodeRuneInString(u) fmt.Println("First rune removed: ", u[sizeFirst:]) _, sizeLast := utf8.DecodeLastRuneInString(u) fmt.Println("Last rune removed: ", u[:len(u)-sizeLast]) fmt.Println("First and last removed:", u[sizeFirst:len(u)-sizeLast])
}</lang> Output:
String: ASCII First byte removed: SCII Last byte removed: ASCI First and last removed: SCI String: Δημοτική First rune removed: ημοτική Last rune removed: Δημοτικ First and last removed: ημοτικ
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>
Java
I solve this problem two ways. First I use substring which is relatively fast for small strings, since it simply grabs the characters within a set of given bounds. The second uses regular expressions, which have a higher overhead for such short strings.
<lang Java>public class RM_chars {
public static void main( String[] args ){ System.out.println( "knight".substring( 1 ) ); System.out.println( "socks".substring( 0, 4 ) ); System.out.println( "brooms".substring( 1, 5 ) ); // first, do this by selecting a specific substring // to exclude the first and last characters System.out.println( "knight".replaceAll( "^.", "" ) ); System.out.println( "socks".replaceAll( ".$", "" ) ); System.out.println( "brooms".replaceAll( "^.|.$", "" ) ); // then do this using a regular expressions }
}</lang>
Results:
night sock room night sock room
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>
Perl 6
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.
<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
say substr('brooms', 1, -1); # strip both first and last characters - sub say 'brooms'.substr(1, -1); # strip both first and last characters - method</lang>
Perl 6 also has chop though it works differently from Perl. There is also p5chop that works like Perls chop.
<lang perl6>my $string = 'ouch'; say $string.chop; # ouc - does not modify original $string say $string; # ouch say $string.p5chop; # h - returns the character chopped off and modifies $string say $string; # ouc</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>
Tcl
<lang tcl>puts [string range "knight" 1 end]; # strip first character puts [string range "write" 0 end-1]; # strip last character puts [string range "brooms" 1 end-1]; # strip both first and last characters</lang>
TUSCRIPT
<lang tuscript> $$ MODE TUSCRIPT str="upraisers" str1=EXTRACT (str,2,0) str2=EXTRACT (str,0,-1) str3=EXTRACT (str,2,-1) PRINT str PRINT str1 PRINT str2 PRINT str3 </lang> Output:
upraisers praisers upraiser upraiser
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>